Re: re.sub and named groups

2009-02-11 Thread Shawn Milochik

 Book recommendation: _Mastering Regular Expressions_, Jeffrey Friedl
 --
 Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

I wholeheartedly second this! The third edition is out now.
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-11 Thread Paul McGuire
On Feb 4, 10:51 am, Emanuele D'Arrigo man...@gmail.com wrote:
 Hi everybody,

 I'm having a ball with the power of regular expression

Don't forget the ball you can have with the power of ordinary Python
strings, string methods, and string interpolation!

originalString = spam:%(first)s ham:%(second)s
print originalString % { first : foo , second :  }

prints

spam:foo ham:

with far fewer surprises and false steps.  (Note: Py3K supports this
same feature, but with different interpolation syntax, which I have
not learned yet.)

Book recommendation: Text Processing in Python, by David Mertz (free
online - http://gnosis.cx/TPiP/), in which David advises against
dragging out the RE heavy artillery until you've at least thought
about using ordinary string methods.

-- Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-11 Thread Rhodri James
On Wed, 11 Feb 2009 21:05:53 -, Paul McGuire pt...@austin.rr.com  
wrote:



On Feb 4, 10:51 am, Emanuele D'Arrigo man...@gmail.com wrote:

Hi everybody,

I'm having a ball with the power of regular expression


Don't forget the ball you can have with the power of ordinary Python
strings, string methods, and string interpolation!


So the moral of this story is take a ball of strings with you for
when you get lost in regular expressions.


--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-10 Thread Aahz
In article 4c7158d2-5663-46b9-b950-be81bd799...@z6g2000pre.googlegroups.com,
Emanuele D'Arrigo man...@gmail.com wrote:

I'm having a ball with the power of regular expression but I stumbled
on something I don't quite understand:

Book recommendation: _Mastering Regular Expressions_, Jeffrey Friedl
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


re.sub and named groups

2009-02-04 Thread Emanuele D'Arrigo
Hi everybody,

I'm having a ball with the power of regular expression but I stumbled
on something I don't quite understand:

theOriginalString = spam:(?Pfirst.*) ham:(?Psecond.*)
aReplacementPattern = \(\?Pfirst.*\)
aReplacementString= foo
re.sub(aReplacementPattern , aReplacementString, theOriginalString)

results in :

spam:foo

instead, I was expecting:

spam:foo ham:

Why is that?

Thanks for your help!

Manu




--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-04 Thread MRAB

Emanuele D'Arrigo wrote:
 Hi everybody,

 I'm having a ball with the power of regular expression but I stumbled
 on something I don't quite understand:

 theOriginalString = spam:(?Pfirst.*) ham:(?Psecond.*)
 aReplacementPattern = \(\?Pfirst.*\)
 aReplacementString= foo
 re.sub(aReplacementPattern , aReplacementString, theOriginalString)

 results in :

 spam:foo

 instead, I was expecting:

 spam:foo ham:

 Why is that?

 Thanks for your help!

The quantifiers eg * are normally greedy; they try to match as much as
possible. Therefore .* matches:

spam:(?Pfirst.*) ham:(?Psecond.*)
   ^

You could use the lazy form *? which tries to match as little as
possible, eg \(\?Pfirst.*?\) where the .*? matches:

spam:(?Pfirst.*) ham:(?Psecond.*)
   ^^

giving spam:foo ham:(?Psecond.*).
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-04 Thread Emanuele D'Arrigo
On Feb 4, 5:17 pm, MRAB goo...@mrabarnett.plus.com wrote:
 You could use the lazy form *? which tries to match as little as
 possible, eg \(\?Pfirst.*?\) where the .*? matches:
 spam:(?Pfirst.*) ham:(?Psecond.*)
 giving spam:foo ham:(?Psecond.*).

A-ha! Of course! That makes perfect sense! Thank you! Problem solved!

Ciao!

Manu

--
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub and named groups

2009-02-04 Thread Yapo Sébastien

 Hi everybody,

 I'm having a ball with the power of regular expression but I stumbled
 on something I don't quite understand:

 theOriginalString = spam:(?Pfirst.*) ham:(?Psecond.*)
 aReplacementPattern = \(\?Pfirst.*\)
 aReplacementString= foo
 re.sub(aReplacementPattern , aReplacementString, theOriginalString)

 results in :

 spam:foo

 instead, I was expecting:

 spam:foo ham:

 Why is that?

 Thanks for your help!

 Manu

   
I think that .* in your replacement pattern matches .*)
ham:(?Psecond.* in your original string which seems correct for a regexp.
Perhaps you should try aReplacementPattern = \(\?Pfirst\.\*\) or use
replace() since your replacement pattern is not a regexp anymore.

Sebastien
--
http://mail.python.org/mailman/listinfo/python-list