Match two alternative or either both words with regex in Java -


in java need find regex able match sentences :

"john smith wants dog , cat"  "john wants fish , snake"  "smith wants spider , horse" 

the same shall not match if instead of "john" or "smith" or "john smith" there's nothing or different word. example shall not match if sentence like:

"jack wants bird , frog" "wants banana , lemon" 

also words in place of animal names shall match capturing groups.

i tried many combination i'm not able find right "formula" because matches if there different words or empty @ beginning. example this:

(john )?(smith )?wants ([a-z]*) , ([a-z]*) 

but works if input:

"wants tiger , lion" 

by finding "tiger" , "lion" groups, while in case don't want match far there's no (the right) name @ beginning.

hope explanation clear enough..... thanks

you need use or between fname , lname :

(?:john|smith|john smith) wants ([a-z]*) , ([a-z]*) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -