1

I am trying to match input data from the user and search if there is a match of this input. for example if the user type : A*B*C* i want to search all word which start with A and contains B and B i tried this code and it;s not working:(get output false)

public static void main(String[] args)
    {

        String envVarRegExp = "^A[^\r\n]B[^\r\n]C[^\r\n]";
        Pattern pattern  = Pattern.compile(envVarRegExp);
        Matcher matcher = pattern.matcher("AmBmkdCkk");
        System.out.println(matcher.find());

    }

Thanks.

3
  • What do you mean by - contains B and B? Did you mean B and C? Commented Nov 26, 2012 at 15:51
  • Can you explain what you are looking for a little more? Can B and C be in any order? How many times can each letter show up? Commented Nov 26, 2012 at 15:54
  • Then Rohit's answer should work perfectly. Commented Nov 26, 2012 at 15:57

3 Answers 3

3

You don't really need Regex here. Simple String class methods will work: -

String str = "AfasdBasdfCa";

if (str.startsWith("A") && str.contains("B") && str.contains("C")) {
    System.out.println("true");
}

Note that this will not ensure that your B and C are in specific order, which I assume you don't need as you have not mentioned anything about that.

If you want them to be in some order (like B comes before C then use this Regex: -

if (str.matches("^A.*B.*C.*$")) {
    System.out.println("true");
}

Note that, . will match any character except newline. So, you can use it instead of [^\r\n], its more clear. And you need to use the quantifier * because you need to match any repetition of the characters before B or C is found.

Also, String.matches matches the complete string, and hence the anchors at the ends.

Sign up to request clarification or add additional context in comments.

6 Comments

@anubhava.. Yeah, but there is nothing specified about the order in the OP.
The issue that i want to build the expression in dynamic way if thee user type abcde i will have different if statement therefore i need regular expression to build it in dynamic way
@user1205079.. Then please specify that clearly in the OP. And what do you want to match in case of any string. Currently it's not very clear what you want from your question.
if (str.matches("^A.*B.*C.*$")) { System.out.println("true"); } is working fine:)
@user1205079.. Ok. But if you are not bothered abotu any specific order, then the first method would be more appropriate here. Because using Regex can be slower than the first approach, due to backtracking.
|
2

I thing you should use * modifier in your regex like this (for 0 or more matches between A & B and then between B & C):

String envVarRegExp = "^A[^\r\n]*B[^\r\n]*C";

EDIT: It appears that you're working off the input coming from your user where user can use asterisk * in inputs. If that is the case consider this:

String envVarRegExp = userInput.replace("*", ".*?");

Where userInput is String like this:

String userInput = "a*b*c*d*e";

10 Comments

I believe you want to add some parenthesis around each statement (A[^\r\n])*(B....)*(C....)* otherwise you are just *'ing the [\r\n].
@Scott yes this regex is intentionally *ing anything that is not \r and not \n i.e. [^\r\n]*
Why are you replacing * with a regex - ".*?".
@RohitJain To make it a valid regex like a.*?b.*?c.*?d.*?e
@anubhava.. Well seems like I was wrong. In fact you got the correct interpretition, I don't know how, from that question, which really didn't explained that it wanted this. Still, you solved the issue. And I would certainly give you a +1 for this. :)
|
2

You need to add quantifiers to your character classes;

String envVarRegExp = "^A[^\r\n]*B[^\r\n]*C[^\r\n]*$";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.