Forum Moderators: coopster

Message Too Old, No Replies

RegEx - Numbers that aren't inside ()

         

Nutter

1:34 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



I'm trying to come up with the RegEx to pull out single numbers and ranges of numbers but not if they're inside parathesis. So 34 and 56-27 would both need to match but (25) and (25-62) wouldn't. Any suggestions where to start?

mcibor

3:02 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I came with such a pattern:

@^[^(][0-9-]*[^)]$@

it will match 1, 298 and 209-280, but also 29- and -0
but not if they start or end with a bracket:

won't accept
(2
nor
340-98)

Hope this helps
Michal

cmarshall

3:22 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you realize that this was your 1024th post?

We need to keep track of these things... :)

Nutter

3:33 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



Hey, I'm pretty happy with this one being #400. Guess I've got some ground to make up :)

I don't know how efficient this is, but what I wound up doing is a preg_replace on anything wrapped in parenthesis and blanking it out.

$string = preg_replace('/\(.*?\)/', '', $string);

It may not be the fastest method, but it seems to work.

cmarshall

3:55 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, 400 ain't nothin' to sneeze at. I just got my star a couple of days ago, myself.

(BTW: I like your alias). ;)

With RegEx, it can be an enormous amount of work to develop a truly efficient, bug-free expression, and is usually not worth it.

If it works for all instances, and doesn't give you performance anxiety, then go for it.