0

This is a general regex question,although I will apply it in PHP.

I am trying to find all files that DO NOT contain strings like -123x456 or -12345x678 meaning that the format is always -INTxINT. (like image resolution) .

This string is a part of the filename. and the "constsnts" are the minus ( - ) sign before the string and the X in the middle ( which can be both capital letters or not )

With the help of THIS have come up with the expression:

^(?:(?!STRING).)*$

or in my case :

^(?:(?!x).)*$

But that will exclude all filenames that contain an x

How can I add the Numbers on both sides of the x when not knowing how many digits they are , but knowing that there COULD be a number before or after that is a part of the filename

(e.g. myfile-x1-456x789 VR. myfile-x12 )

I need a separate solution for x in specific , or for X | x ...

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • You don't the negative assertion if you were to use `preg_grep()` with `PREG_GREP_INVERT` on a filename list. Also are you just asking for `\d+x\d+`? – mario Mar 19 '13 at 01:46
  • Thank you mario for your input , but I am not sure what `\d+x\d+` means (no regex expert here ... ) – Obmerk Kronen Mar 19 '13 at 01:47
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Mar 19 '13 at 01:52

2 Answers2

1

You can use an inverted search (on linux with the grep tool and -v or --invert-match argument) or as @mario suggested with preg_grep() in PHP.

To answer your question use this RegEx: ^(?!.*?-\d+[xX]\d+)(.*)$ explained demo

To match both x and X you can also use (x|X) or just x if used with the case insensitive regex modifier i.

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
  • Thanks ! it works .. also a big +1 for using that website .. the feature where it is automatically explains the regex is great ! – Obmerk Kronen Mar 19 '13 at 02:08
  • as for your edit - this is another confusion I always have , which is the difference between `Xx` and `x|X` . but i guess that is for another question ... thanks again .. – Obmerk Kronen Mar 19 '13 at 02:12
  • 1
    `[]` denotes a character set, will match one character listed there so I listed both x and X. `|` is OR and will try to match the first one, if it fails it will try to match the second – CSᵠ Mar 19 '13 at 02:14
0

Why even use PHP on this at all? Use grep's -L option to find files that don't match the regex.

grep -L -- '-\d+x\d+' *

You have to put the regex after a -- so that grep does not think that the regex is a command line option.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • Thanks for the info . I am on win machine , but I will give a try when I will get a chance to switch to linux . I will be using php because it is a part of a website ( ? ) – Obmerk Kronen Mar 19 '13 at 05:10