Search code examples
regexregex-lookaroundsregex-group

RegEx capture specific string


Hi everyone, I have a problem. I'm trying to use regex to get all lines that are not end with

_0.jpg
_s.jpg
_m.jpg
_l.jpg


Example Lines:


9Uikt/ifehr54mg__0.jpg
9Uikt/idg4hdmg2_s.jpg
9Uikt/igdffgggfmg4_m.jpg
9Uikt/img3teg3gegg7_l.jpg
9Uikt/imgerhw45h70.jpg
9Uikt/imggq4ge37s.jpg
9Uikt/img3f37m.jpg
9Uikt/img34g3f7l.jpg
9Uikt/imgf3f34t4t73l_.jpg
9Uikt/imgf3f34t4t73l_2.jpg

The bold ones I am trying to get.

Between 9Uikit/ and .jpg any character can happen, except the characters that are not allowed for file names

"*:<>?/\|

 I have tried this code

.*(_(?![0-9][a-zA-Z])).*\.jpg

Solution

  • You can use

    ^(?!.*_[0sml]\.jpg$).+\.jpg$
    

    See the regex demo

    Details

    • .* - any zero or more chars other than line break chars, as many as possible
    • (?!.*_[0sml]\.jpg$) - a negative lookahead that fails the match if, immediately to the right of the current location, there are
      • .* - any zero or more chars other than line break chars, as many as possible
      • _ - an underscore
      • [0sml] - a 0, s, m, l char
      • \. - a dot
      • jpg - jpg string
      • $ - end of string anchor
    • .+\.jpg$ - any one or more chars other than line break chars, as many as possible, .jpg string and end of string.

    Or, .*(?<!_[0sml])\.jpg$ if you can afford a lookbehind:

    .*(?<!_[0sml])\.jpg$
    

    See this regex demo. Details:

    • .* - any zero or more chars other than line break chars, as many as possible
    • (?<!_[0sml]) - no _ and 0, s, m or l char immediately on the left is allowed -\.jpg$ - .jpg at the end of string.