Forum Moderators: coopster

Message Too Old, No Replies

Help with regular expression?

Want to strip everything between brackets.

         

d1lordbry

10:41 pm on Aug 8, 2007 (gmt 0)

10+ Year Member



Hey there, I have a love/hate relationship with regular expressions. I love how powerful they are and hate how mindnumbingly complex they can be to put together. Any help would be greatly appreciated here. I'm trying to do this one thing. If the input string is:

Abc (123 abc stuff) 1xyz

I want to strip out the ()'s and everything between them to end up with:

Abc 1xyz

Any help would be greatly appreciated. Thanks.

- Bry

d1lordbry

12:15 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Also if there's a way that any of you know how to take this:

ABC ID #: 123456 $123.45

... and turn it into this...

ABC $123.45

... I would also appreciate that. :)

- Bry

phranque

12:42 am on Aug 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it's hard to tell from one example what the pattern really is, but this might help to start...

Abc (123 abc stuff) 1xyz

the following regular expression:
^([^(]*)\([^)]*\)([^)]*)$
will return everything up to but not including the first '(' in $1 and everything after the last ')' in $2.

ABC ID#: 123456 $123.45

the following regular expression:
^([^ ]*) [^$]*(\$[^$]*)$
will return everything up to but not including the first ' ' in $1 and everything after and including the last '$' in $2.
(strictly speaking, it probably won't match if you have more than one '$' in the string so you may have to further tweak the regexp if you have these cases.)

d1lordbry

1:55 pm on Aug 9, 2007 (gmt 0)

10+ Year Member



Thanks. I'll use that to get started on my way. These things can be so confusing though.