I'm working with Perl to search and match for strings on each line that match a criteria and would like to omit the lines that contain a specific string. What I mean is: Say I'm matching the string Mouse, but I'd like to omit if the line matches X123Y. Either strings can be found anywhere on the line.
Stackoverflow Mouse forum. <--Match
Stackoverflow -Mouse- forum. <--Match
Stackoverflow X123Y forum Mouse. <--Should not Match
Stackoverflow XYZ forum Mouse. <--Should not Match
I hoped this would solve it since I'm using negative lookahead but doesn't seem to do the trick.
(?i)(\WMouse\W|(?!(X123Y|XYZ)).*$)
I'm doing something fundamentally wrong I suppose, but cannot see it now.
Any help?
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.
This regex should work for you:
^(?=.*?Mouse)(?:(?!(?:X123|XYZ)).)*$
You can use the discard technique to keep with the content you want and discard the patterns you don't.
For example, using this regex:
.*X123Y.*|.*XYZ.*|(.*Mouse.*)
You will grab the content for the rightest pattern and discard the others..
Working demo
The idea is to use:
discard patt 1 | discard patt 2 | discard patt n | (grab this pattern)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With