5

i want to check 2 patterns using regex.

can i check those both patterns in the same time (like if(condition1 | condition2) condition).

any idea?

0

2 Answers 2

12

You can do it exactly the way you did, with pipe separating the two+ expressions

For instance: The regular expresion (abc)|(def) would match abc OR def

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

1 Comment

That is the syntax. The regular expression pattern1|pattern2 means "match pattern1 or pattern2".
4

It really depends - namely, you can design your regex with "or" modifiers like this "(match this)|(or this)". If you use carefully designed regex, you only need to do this:

Pattern p1 = Pattern.compile(regex)
Matcher m = p1.matcher(searchstring)

Once. This is probably the most efficient way to go about things. The other option is to run two matcher/pattern object pairs, run find operations until find returns false than count the number of outputs. If they're both > 0 you're in business. The other option is if you only need one or more matches, to do:

if ( matcher1.find() & matcher2.find() )
{
    ...
}

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.