How to find non-matching strings with Regex in Sublime?

Viewed 76

I have this text:

    <Path Fill="None"
        PathData="M244.87,363.97 L245.38,363.91 M245.38,363.91 L245.46,363.84 M245.46,363.84 L245.52,363.75 M245.52,363.75 L245.54,363.7 M245.54,363.7 L246.07,370.18 M246.07,370.18 L245.95,370.25 M245.95,370.25 L245.8,370.37 M245.8,370.37 L245.63,370.54 M245.63,370.54 L245.52,370.73 M245.52,370.73 L245.42,370.9 M245.42,370.9 L245.17,368.03 M245.17,368.03 L244.87,363.97"
        Stroke="#898989" StrokeWidth="0.5"/>
    <Path Fill="None"
        PathData="M247.4,371.21 L247.49,371.16 M247.49,371.16 L247.91,371.13 M247.91,371.13 L249.74,371.01 M249.74,371.01 L252.52,370.82 M252.52,370.82 L252.72,370.83 M252.72,370.83 L252.72,370.84 M252.72,370.84 L252.71,370.89 M252.71,370.89 L252.72,370.95 M252.72,370.95 L252.75,371.38 M252.75,371.38 L251.86,371.44 M251.86,371.44 L249.62,371.63 M249.62,371.63 L247.55,371.79 M247.55,371.79 L247.51,371.35 M247.51,371.35 L247.47,371.28 M247.47,371.28 L247.42,371.22 M247.42,371.22 L247.4,371.21"
        Stroke="#878787" StrokeWidth="0.5"/>
    <Path Fill="None"
        PathData="M246.46,372.67 L246.47,372.05 M246.47,372.05 L246.47,372.05 M246.47,372.05 L246.52,372.07 M246.52,372.07 L246.58,372.09 M246.58,372.09 L247.44,372.02 M247.44,372.02 L248.68,371.91 M248.68,371.91 L248.81,373 M248.81,373 L248.07,373.06 M248.07,373.06 L247.88,373.07 M247.88,373.07 L248.54,379.11 M248.54,379.11 L247.62,379.18 M247.62,379.18 L247.2,379.21 M247.2,379.21 L247.15,379.24 M247.15,379.24 L247.12,379.27 M247.12,379.27 L247.06,379.17 M247.06,379.17 L246.83,376.84 M246.83,376.84 L246.46,372.67"
        Stroke="#898989" StrokeWidth="0.5"/>

And I am trying to find and delete the paths which are not of a certain color, i.e. - #898989. I would like to use regex to find the non-matching strings.

I am trying the following:

.*(<Path Fill).*(\r\n|\r|\n).*(\r\n|\r|\n).*(?!#898989).*(\r\n|\r|\n)

But this returns the same as the one I would use to find the matching strings:

.*(<Path Fill).*(\r\n|\r|\n).*(\r\n|\r|\n).*(#898989).*(\r\n|\r|\n)

I thought the ?! was a negative lookahead, and would exclude those strings. It seems to not change the results, though.

Any help?

2 Answers

There are many regex solutions to your problem. Lets first discuss why the regex you proposed does not work as expected.


Problem

.*(<Path Fill).*(\r\n|\r|\n).*(\r\n|\r|\n).*(?!#898989).*(\r\n|\r|\n)

The problem occurs at the part

.*(?!#898989).*(\r\n|\r|\n)

The regex simply says match as much of anything as you can. After matching, check if at the current position there is no #898989. Then again....

The match as much of anything as you can is causing the problem. The first .* is actually capturing the whole line.

        Stroke="#898989" StrokeWidth="0.5"/>

Then (?!#898989) comes into play which will succeed since after > there is no #898989. To make it obvious, change the regex to -

.*(?:<Path Fill).*[\r\n].*[\r\n](.*)(?!#898989).*

This regex does the same thing. In this regex, (\r\n|\n|\r) is replaced with [\r\n]. Nothing is being captured by the starting brackets (?:<Path Fill). However, this time the .* before #898989 is surrounded by (...) to highlight the text being captured by it.

Observe the yellow lines to see what is being captured by the .* before the #898989. Here is the link: https://regex101.com/r/2R54uW/1


Correction

As already mentioned in the comments, the regex can be corrected by forcing the .* to stop at Stroke=" and then making the position check.

.*(?:<Path Fill).*[\r\n].*[\r\n].*Stroke=\"(?!#898989).*[\r\n]

Here is another regex that does the same thing -

.*(?:<Path Fill).*[\r\n].*[\r\n]((?!#878787).)*/>

Final Thoughts

Try using [\r\n] in place of (\r\n|\r|\n) since character class is faster than alternation.

If you have any additional doubts please comment.

If there can not be a < and > char in the Path, you could assert that the color does not occur before the closing />

<Path Fill(?![^<>]*#898989[^<>]*/>)[^<>]*/>

Regex demo

If it should be the Stroke specifically

<Path Fill(?![^<>]*Stroke="#898989"[^<>]*/>)[^<>]*/>

Regex demo

Related