Regular expression matches multiple lines but not defined

I work with Perl to find and match strings in each line that matches the criteria, and would like to omit lines that contain a specific line. I mean: Let's say I match the string Mouse, but I would like to skip if the string matches X123Y. Any line 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 was hoping this would solve it, as I use a negative look, but it doesn't seem to be a trick.

 (?i)(\WMouse\W|(?!(X123Y|XYZ)).*$) 

I am doing something fundamentally wrong, I suppose, but I donโ€™t see it now.

Any help?

+5
source share
2 answers

This regex should work for you:

 ^(?=.*?Mouse)(?:(?!(?:X123|XYZ)).)*$ 

RegEx Demo

+4
source

You can use the reset technique to save the content you want and discard templates that you don't have.

For example, using this regex:

 .*X123Y.*|.*XYZ.*|(.*Mouse.*) 

You grab the content for the right template and drop the rest.

Working demo

enter image description here

The idea is to use:

 discard patt 1 | discard patt 2 | discard patt n | (grab this pattern) 
+1
source

Source: https://habr.com/ru/post/1200355/


All Articles