Regular expression, "except end of string", question.

D

Derek Basch

Hello,

I have a string like:

string = "WHITE/CLARET/PINK/XL"

which I need to alter to this format:

string = "WHITE-CLARET-PINK/XL"

I developed the below functions to do so. However, something is wrong
with my regular expression. It currently matches with the following:

/CLARET
/PINK
/XL

I thought perhaps I could exclude the match that includes the end of the
string with someting like this:

r"([/]\w+[^$])"

but that didnt work either. Can anyone tell me how to exclude the last
match? The one with the end of the string "/XL".

Thanks a million,
Derek Basch

------------------------------------------------
import re

string = "WHITE/CLARET/PINK/XL"

def replace_fs(thematch):
thematch = thematch.group(1)
thematch = "-" + thematch[1:]
return thematch

bs_regex = re.compile(r"([/]\w+)").sub(replace_fs, str(string))
print bs_regex
 
D

David Eppstein

Derek Basch said:
string = "WHITE/CLARET/PINK/XL"

which I need to alter to this format:

string = "WHITE-CLARET-PINK/XL"

Do you really need regexps for this?
'WHITE-CLARET-PINK/XL'
 
P

Peter Hansen

Derek said:
I have a string like:

string = "WHITE/CLARET/PINK/XL"

which I need to alter to this format:

string = "WHITE-CLARET-PINK/XL"

I developed the below functions to do so. However, something is wrong
with my regular expression.

As they say, if you have a problem and think a regular expression
is the best way to solve it, you might now have two problems...

Do you always want to exclude the last "/" ? How about this
instead:

s = 'white/claret/pink/xl'

s2 = s.split('/') # temporary list

# join all but last with hyphens, add last after slash
s = '-'.join(s2[:-1]) + '/' + s2[-1]

# s is now 'white-claret-pink/xl'

Depending on what the format really is (e.g. is the /XL
actually optional?) it might be simpler or harder than
this.

An re can be made to work too, probably, but perhaps it
won't be very clear.

-Peter
 
P

Peter Hansen

David said:
Do you really need regexps for this?


'WHITE-CLARET-PINK/XL'

Dang! I started with split('/', 2) but stared at the
result trying to figure out how the heck to join it
to get the right result and ended up punting. :-(

-Peter
 
D

Derek Basch

Do you really need regexps for this?

'WHITE-CLARET-PINK/XL'

Thanks Peter and David,

It always comforting to know that a really simple solution exists after
you waste a couple of hours messing with Regular Expressions. :)

Cheers,
Derek
 
D

David Fisher

Peter Hansen said:
Dang! I started with split('/', 2) but stared at the
result trying to figure out how the heck to join it
to get the right result and ended up punting. :-(

-Peter

$ python
Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
[GCC 3.3.1 (cygming special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
 
D

Doug Holton

Derek said:
string = "WHITE/CLARET/PINK/XL"

which I need to alter to this format:

string = "WHITE-CLARET-PINK/XL"

You need the (?!...) operator: http://docs.python.org/lib/re-syntax.html

import re
#replace "/" with "-" if not followed by a word at the end of the string
print re.sub(r"/(?!\w+$)",r"-",s)

but like they said, join and split are better here instead of
using regular expressions.
 
P

Peter Hansen

Derek said:
Thanks Peter and David,

It always comforting to know that a really simple solution exists after
you waste a couple of hours messing with Regular Expressions. :)

And, usually, there's an even simpler solution than the first
couple of responses, as David #2 showed. :) His has the added
advantage of not building a temporary list too.

-Peter
 
A

Aahz

It always comforting to know that a really simple solution exists after
you waste a couple of hours messing with Regular Expressions. :)

'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.' --Jamie Zawinski
 
D

Derek Basch

'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.' --Jamie Zawinski

(?!...)
Matches if ... doesn't match next. This is a negative lookahead
assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if
it's not followed by 'Asimov'.

That wasn't covered in my trusty (outdated) Python 2.1 Bible. I should
have gone straight to the Library reference. Thanks.
 
P

Peter Otten

Peter said:
Dang! I started with split('/', 2) but stared at the
result trying to figure out how the heck to join it
to get the right result and ended up punting. :-(

Strictly speaking the problem is underspecified, and your original solution
might still be the best if claret is out tomorrow.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top