Re: re.sub() problem (regular expression)

2007-12-13 Thread Rick Dooling
On Dec 13, 9:00 pm, Davy <[EMAIL PROTECTED]> wrote:
>
> What's "\1" and the whole re.sub() mean?
>

Read about backreferences here:

http://www.regular-expressions.info/brackets.html

Also see the entry on parentheses here:

http://docs.python.org/lib/re-syntax.html

rick

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


re.sub() problem (regular expression)

2007-12-13 Thread Davy
Hi all,

I have read a re.sub() that confused me.

s = 'P & Q'
s = re.sub(r'([a-zA-Z0-9_.]+)', r'Expr("\1")', s)

What's "\1" and the whole re.sub() mean?

Best regards,
Davy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub problem

2006-03-31 Thread RunLevelZero
Glad I could help.

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


Re: re.sub problem

2006-03-31 Thread veracon
Thanks a lot! Compiling with re.DOTALL did fix my problem for the most
part; there still are a few problems with my code, but I think I can
fix those myself.

Again, thanks!

> Okay I just woke up and haven't had enough coffee so if I'm off here
> please forgive me.  Are you saying that if there is an emptly line then
> it borks?  If so just use re.S ( re.DOTALL ) and that should take care
> of it.  It will treat the ( . ) special.  Otherwise it ignores new
> lines.

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


Re: re.sub problem

2006-03-31 Thread RunLevelZero
Okay I just woke up and haven't had enough coffee so if I'm off here
please forgive me.  Are you saying that if there is an emptly line then
it borks?  If so just use re.S ( re.DOTALL ) and that should take care
of it.  It will treat the ( . ) special.  Otherwise it ignores new
lines.

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


Re: re.sub problem

2006-03-31 Thread veracon
Actually, it happens in general when there is more than one linebreak
between the open and close statements; not only when there are empty
lines.

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


re.sub problem

2006-03-31 Thread veracon
I'm trying to make a (tiny) template system (Cheetah and like have far
more than what I need), but I've run into a problem. To simplify
everything, I've decided to make for loops matching the indentation
level of the open and close statements; it appears to work fine, but
apparently it chokes once there are empty lines inside the string being
replaced in.

It's a bit hard to explain, so I'll just show an example:
stm = re.compile('\n(\s+)\{\{for (.+?) in
(.+?)\}\}\n?(.+?)\n\\1\{\{end for\}\}', re.M)
data = re.sub(stm, self.handle_for, data)

I do have a self.handle_for, and I can see that it's not called if I
give it the following string:
[... (not beginning of actual string) ]
  {{for baz in bar}}
  foo:{baz}
b

  {{end for}}

There, nothing is matched; if there wasn't an empty line, it would
match something.

What am I doing wrong?

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