Python's regular expression help

G

goldtech

Hi,
Trying to start out with simple things but apparently there's some
basics I need help with. This works OK:'sss'
....
But two questions:

How can I operate a regex on a string variable?
I'm doing something wrong here:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

How do I implement a regex on a multiline string? I thought this
might work but there's problem:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
Thanks for the newbie regex help, Lee
 
D

Dodo

Le 29/04/2010 20:00, goldtech a écrit :
Hi,
Trying to start out with simple things but apparently there's some
basics I need help with. This works OK:
'sss'
...
But two questions:

How can I operate a regex on a string variable?
I'm doing something wrong here:

Traceback (most recent call last):
File "<pyshell#15>", line 1, in<module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

How do I implement a regex on a multiline string? I thought this
might work but there's problem:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in<module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

Thanks for the newbie regex help, Lee

for multiline, I use re.DOTALL

I do not know match(), findall is pretty efficient :
['LINK']

Dorian
 
M

MRAB

goldtech said:
Hi,
Trying to start out with simple things but apparently there's some
basics I need help with. This works OK:
'sss'
...
But two questions:

How can I operate a regex on a string variable?
I'm doing something wrong here:

Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
Look closely: the regex contains 3 letter 's', but the string referred
to by f has only 2.
How do I implement a regex on a multiline string? I thought this
might work but there's problem:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

Thanks for the newbie regex help, Lee

The string contains a newline between the 'b' and the 's', but the regex
isn't expecting any newline (or any other character) between the 'b' and
the 's', hence no match.
 
T

Tim Chase

Trying to start out with simple things but apparently there's some
basics I need help with. This works OK:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in<module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

'absss' != 'abss'

Your regexp looks for 3 "s", your "f" contains only 2. So the
regexp object doesn't, well, match. Try

f = 'absss'

and it will work. As an aside, using raw-strings for this text
doesn't change anything, but if you want, you _can_ write it as

f = r'absss'

if it will make you feel better :)
How do I implement a regex on a multiline string? I thought this
might work but there's problem:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in<module>
m.group(0)
AttributeError: 'NoneType' object has no attribute 'group'

Well, it depends on what you want to do -- regexps are fairly
precise, so if you want to allow whitespace between the two, you
can use

r = re.compile(r'(ab*)\s*(sss)')

If you want to allow whitespace anywhere, it gets uglier, and
your capture/group results will contain that whitespace:

r'(a\s*b*)\s*(s\s*s\s*s)'

Alternatively, if you don't want to allow arbitrary whitespace
but only newlines, you can use "\n*" instead of "\s*"

-tkc
 
G

goldtech

'absss' != 'abss'

Your regexp looks for 3 "s", your "f" contains only 2.  So the
regexp object doesn't, well, match.  Try

   f = 'absss'

and it will work.  As an aside, using raw-strings for this text
doesn't change anything, but if you want, you _can_ write it as

   f = r'absss'

if it will make you feel better :)


Well, it depends on what you want to do -- regexps are fairly
precise, so if you want to allow whitespace between the two, you
can use

   r = re.compile(r'(ab*)\s*(sss)')

If you want to allow whitespace anywhere, it gets uglier, and
your capture/group results will contain that whitespace:

   r'(a\s*b*)\s*(s\s*s\s*s)'

Alternatively, if you don't want to allow arbitrary whitespace
but only newlines, you can use "\n*" instead of "\s*"

-tkc

Yes, most of my problem is w/my patterns not w/any python re syntax.

I thought re.S will take a multiline string with any spaces or
newlines and make it appear as one line to the regex. Make "/n" be
ignored in a way...still playing w/it. Thanks for the help!
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top