Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regular expression, "except end of string", question.

Reply
Thread Tools

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

 
 
Derek Basch
Guest
Posts: n/a
 
      06-16-2004
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
 
Reply With Quote
 
 
 
 
David Eppstein
Guest
Posts: n/a
 
      06-16-2004
In article <(E-Mail Removed)>,
Derek Basch <(E-Mail Removed)> wrote:

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

>>> string = "WHITE/CLARET/PINK/XL"
>>> '-'.join(string.split('/',2))

'WHITE-CLARET-PINK/XL'

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-16-2004
Derek Basch wrote:

> 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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-17-2004
David Eppstein wrote:

> Do you really need regexps for this?
>
>>>>string = "WHITE/CLARET/PINK/XL"
>>>>'-'.join(string.split('/',2))

>
> '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
 
Reply With Quote
 
Derek Basch
Guest
Posts: n/a
 
      06-17-2004
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> In article <(E-Mail Removed)>,
> Derek Basch <(E-Mail Removed)> wrote:
>
> > 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?
>
> >>> string = "WHITE/CLARET/PINK/XL"
> >>> '-'.join(string.split('/',2))

> '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
 
Reply With Quote
 
David Fisher
Guest
Posts: n/a
 
      06-17-2004
Peter Hansen <(E-Mail Removed)> writes:

> David Eppstein wrote:
>
> > Do you really need regexps for this?
> >
> >>>>string = "WHITE/CLARET/PINK/XL"
> >>>>'-'.join(string.split('/',2))

> > '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


$ 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.
>>> string = "WHITE/CLARET/PINK/XL"
>>> string.replace('/','-',2)

'WHITE-CLARET-PINK/XL'
>>>



><{{{*>

 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      06-17-2004
Derek Basch wrote:
>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.
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-17-2004
Derek Basch wrote:

> 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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      06-17-2004
In article <(E-Mail Removed)>,
Derek Basch <(E-Mail Removed)> wrote:
>
>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
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith, c.l.py
 
Reply With Quote
 
Derek Basch
Guest
Posts: n/a
 
      06-17-2004
In article <caqvac$t1s$(E-Mail Removed)>, (E-Mail Removed) says...
> In article <(E-Mail Removed)>,
> Derek Basch <(E-Mail Removed)> wrote:
> >
> >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
>


(?!...)
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with switch configuration, ( 3 3550's, 5 2950's end to end ) ec Cisco 3 07-25-2006 10:30 AM
using translate as a REAL end-to-end x25 to tcp gateway? RedRat Cisco 1 02-01-2006 09:21 PM
Re: Private LAN: why should the gateway address be at the low end of the range, rather than at the high end. Ted Jones Cisco 11 11-04-2005 05:56 AM
Measure delay end-to-end Dave Cisco 1 07-20-2004 12:51 PM
is there a difference between CIR and CIR+end to end clear channel connection? ike lozada Cisco 0 05-27-2004 02:34 AM



Advertisments