I am currently coding a .net windows app using vb.net.
I am trying to pass a regular expression to Regex.Match
to extract certain texts from an article. How do I write an if condition within a regular expression? I read this regular expression cheat sheet, according to which a condition can be stated using <?()>
, but no example was given.
For example, I have following text:
"Mary have banana. Mary have apple. Mary have NO pear."
I can use the following expression to take out (1) banana
, (2) apple
, and (3) NO pear
:
mary have (.+?\.)+?
But if I want to extract only the fruits that mary
has, namely (1) banana
and (2) apple
, I guess I would need to add a condition in the (.+?\.)+?
part, right? How do I list the condition in a regular expression?
Please assist, thank you!
Try this here:
You can try it online here: regexr.com?2thid
The first part is a negative lookahead assertion, that means this regex will not match if there is "Mary have NO". Otherwise it will put the word after "Mary have" into the first capturing group.
Here in the Perlretut (assuming its the same for .net) the condition part is explained, but I think my solution is simpler.