Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:30:02+00:00 2026-06-17T16:30:02+00:00

Conditions: there are many rules ,maybe hundreds, which are like : {aab*, aabc*, aabcdd*,

  • 0

Conditions:

  1. there are many rules ,maybe hundreds, which are like :
    {aab*, aabc*,
    aabcdd*, dtctddds*,
    *ddt*,
    *cddt*,
    *bcddt*,
    *t,
    *ttt,
    *ccddttt}

  2. each time I will get one string, then I should find the longest matched rule.

Examples:

example 1.string is aabcddttt the matched rule should be: aabcdd*

example 2. string is accddttt the matched rule should be *ccddttt

Question:
I don’t want to use the rules in a long array to match the string one by one,that is inefficient method.maybe I should use the string as a regex to match the hundred rules.But yet I can’t find a elegant way to solve this problem.

  1. Can I use some regexes to get the result?
  2. Which is the best/fastest way to match?

Java, plain C or shell are preferred,please don’t use C++ STL

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:30:03+00:00Added an answer on June 17, 2026 at 4:30 pm

    You could try matching them all at once with a brackets around each sub-rule. You could use the group to determine which matched.

    public static void main(String... ignored) {
        for (String test : "aabaa,wwwaabcdddd,abcddtxyz".split(",")) {
            System.out.println(test + " matches " + longestMatch(test, "aab*", "aabc*", "aabcdd*", "dtctddds*", "ddt"));
        }
    }
    
    public static String longestMatch(String text, String... regex) {
        String[] sortedRegex = regex.clone();
        Arrays.sort(sortedRegex, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.length() - o1.length();
            }
        });
        StringBuilder sb = new StringBuilder();
        String sep = "(";
        for (String s : sortedRegex) {
            sb.append(sep).append('(').append(s).append(')');
            sep = "|";
        }
        sb.append(")");
        Matcher matcher = Pattern.compile(sb.toString()).matcher(text);
        if (matcher.find()) {
            for (int i = 2; i <= matcher.groupCount(); i++) {
                String group = matcher.group(i);
                if (group != null)
                    return sortedRegex[i - 2];
            }
        }
        return "";
    }
    

    prints

    aabaa matches aabc*
    wwwaabcdddd matches aabcdd*
    abcddtxyz matches ddt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we have many rules which cannot be normalized, there would be a long
There are various answers on Stack Overflow which explain the conditions under which tail
I have this below code(this is a sample, there are many more other conditions
I've seen many examples with multiple conditions related to each other, but there are
Is there a tool that analyses .NET code and finds race conditions? I have
Is there a way to create a condition like this? @products = Product.find(:all, :limit
When is exception handling more preferable than condition checking? There are many situations where
I need a robust/reliable/elegant way to handle any error conditions while re-loading firewall rules,
I've been recently playing with implementing AJAX into WordPress. I know there is many
There are many tables as the following: table_2010 table_2009 table_2008 table_2007 . . Using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.