preg_replace?

Ian Bicking ianb at colorstudy.com
Wed Apr 17 16:51:50 EDT 2002


On Wed, 2002-04-17 at 15:09, Philipp Lenssen wrote:
> I am porting a program that I already wrote in VBScript and PHP.
> My current task: I need to replace everything inbetween square brackets with
> "0". In VBScript I did this "by hand" (looping, inString and so on). In PHP
> somebody helped me with the following solution:
> 
>     $s = preg_replace("%\[[^\]]*\]%", "0", $s);
> 
> Even though I've been reading through regex-tutorials in the meantime, I'm
> still not familiar with it. But is there a simple rule how to convert the
> above into Python code? Not only would it solve my current problem but I
> already got some helpful regex's for PHP and could reuse them in the future.
> 
> Anyway, the best (or worst) I did so far is:
>     pattern = re.compile(r'(\[[^\]]*\])')
>     pattern.sub('0', s)
> 
> I'm not convinced this remotely resembles how it's done in Python; it
> doesn't cause an error, doesn't do what I want either.
> 
> Here's my hoped before after:
>     s = "Hello [test] world [bla bla]..." # before regular expression
>     s = "Hello 0 world 0..." # after

That's what it does for me.  What's the problem?  

You don't need the parenthesis in your regex, but otherwise it looks
perfectly fine.  You could also do it as r'\[.*?\]', where the ? makes
the .* match as short a string as possible, as opposed to as long a
string as possible.  But these are stylistic -- your code is fine.

Maybe you wanted to do:

pattern = re.compile(r'(?<=\[)[^\]]+(?=\])')

The (?<=...) and (?=...) do look-behind and look-ahead matching.  You
can also do:

result = pattern.sub('[0]', s)

By using functions instead of strings for your substitution argument,
you can do far more advanced things, like substituting something into
the braces based on the text inside the braces.

  Ian







More information about the Python-list mailing list