开发者

can we check multiple patterns using regex in java?

开发者 https://www.devze.com 2022-12-22 22:42 出处:网络
i want to check 2 patterns using regex. can i check those both patterns in the same time (like if(condition1 开发者_开发问答| condition2) condition).

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?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消