0

How would I go about executing a block of code if the pattern matches or execute another block of code if the pattern does not match?

String input = "abc";

final String mainRegex = "(.*?)(&!|&|==)";

final Matcher matcher = Pattern.compile(mainRegex).matcher(input);


I have tried

if(matcher1.matches())
{
    execute this block
}

else
{
    execute this block
}

But it always executes the else block. Even when the input is a>b&!c<d.

1
  • 3
    I'd suggest testing your regex with a tool like regexpal.com. If the matcher keeps telling you that your input does not match the regex, it probably means that none of your input matches the regex. Commented Aug 30, 2012 at 19:39

1 Answer 1

6

Your code is correct but your regular expression is wrong. It doesn't match either of your examples.

Perhaps you could try this regular expression instead:

final String mainRegex = "(.*?)(&!|&|==)(.*)";

See it working online: ideone

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

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.