tags:

views:

102

answers:

3

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?

+3  A: 

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

Daniel Silveira
can u send me the syntax plz..
Praveen Chandrasekaran
That is the syntax. The regular expression `pattern1|pattern2` means "match pattern1 or pattern2".
Jefromi
A: 

Might be little out of context...but I always use http://www.txt2re.com/ to frame my patterns and code. Plz take a look

HonorGod
+3  A: 

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() )
{
    ...
}
Ninefingers