Forum Moderators: coopster

Message Too Old, No Replies

Matching this, but not this

         

ntbgl

4:41 pm on Jul 31, 2008 (gmt 0)

10+ Year Member



I'm trying to use PHP to match a string, but I'm having problems with the regular expression.

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_-]*)

janharders

4:44 pm on Jul 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hm, use two matches. I don't see any other way right now.

PHP_Chimp

6:06 pm on Jul 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



([A-Za-z0-9_-]*)

Will also match nothing, as * is 0 or more. So you want

([A-Za-z0-9_-]+)
// or
([\w-]+) // as \w will cover a-z, A-Z, 0-9 and _

As this will force at least 1 character.

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
}

ntbgl

2:24 am on Aug 1, 2008 (gmt 0)

10+ Year Member



Thank you for the replies.

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" ?

janharders

8:28 am on Aug 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no, unfortunately, you can't. maybe one could do something with some evil look-aheads oder look-behinds, but that's extended reg exps and my head is getting dizzy just thinking about it. If you want to get into that, check out [perldoc.perl.org...]

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.

ntbgl

1:18 pm on Aug 5, 2008 (gmt 0)

10+ Year Member



Thank you very much for your reply.

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"