Forum Moderators: coopster
I'm trying to match any string that comprises of letters A-Z regardless of capitalization, all numbers, an underscore or a dash. That bit of code works great.
But now I'm trying to exclude the match "example".
Any ideas?
Thanks
([A-Za-z0-9_-]*)
([A-Za-z0-9_-]*)
([A-Za-z0-9_-]+)
// or
([\w-]+) // as \w will cover a-z, A-Z, 0-9 and _
If you want to exclude 'example' then you could use something like this:
if ($string != 'example' && preg_match('%([\w-]+)%', $string)) {
echo 'All working';
}
else {
// whatever
}
I did change my string to [\w-]+ but I was hoping to add that exclusion to my regex to avoid other coding issues.
Right now I have:
preg_match("/([\w-]+)\.jpg/isx",$file,$img);
Each $file has atleast one string that I want to match. The first one is what I want to match, but sometimes it matches the second one called example.jpg. I was hoping to exclude example from the string so I don't have an endless if else loop if it keeps trying to match example.
I know the regex is wrong, but can't I do something like:
"/([\w-[^example]]+)\.jpg/isx" ?
I don't know _if_ you could solve your problem that way or if php supports it, but it's always been something that does something cool after I did something I didn't think of.
I thought of an extremly unattractive work around, maybe.
I guess something like this would be the only way to achieve a regex statement that would match everything but that one word.
"/(([\w-]{1,6})¦([\w-]{8,})¦([A-DF-Za-df-z0-9_-])([A-WYZa-wyz0-9_-])....)\.jpg/isx"