re.match and non-alphanumeric characters

T

The Web President

Dear all,

this is really driving me nuts and any help would be extremely
appreciated.

I have a string that contains some numeric data. I want to isolate
these data using re.match, as follows.

bogus = "IFC(35m)"
data = re.match(r'(\d+)',bogus)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
File "C:\Documents and Settings\Mattia\Desktop\Neeltje\read.py",
line 20, in <module>
print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?
 
R

r

Dear all,

this is really driving me nuts and any help would be extremely
appreciated.

I have a string that contains some numeric data. I want to isolate
these data using re.match, as follows.

bogus = "IFC(35m)"
data = re.match(r'(\d+)',bogus)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
  File "C:\Documents and Settings\Mattia\Desktop\Neeltje\read.py",
line 20, in <module>
    print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?

try re.search or re.findall
re.match is only at the beginning of a string
i almost never use it(4, 6)
 
M

MRAB

Dear all,

this is really driving me nuts and any help would be extremely
appreciated.

I have a string that contains some numeric data. I want to isolate
these data using re.match, as follows.

bogus = "IFC(35m)"
data = re.match(r'(\d+)',bogus)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
  File "C:\Documents and Settings\Mattia\Desktop\Neeltje\read.py",
line 20, in <module>
    print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?

re.match() anchors the match at the start of the string. What you need
is re.search(). It's all in the documentation! :)
 
D

Diez B. Roggisch

The said:
Dear all,

this is really driving me nuts and any help would be extremely
appreciated.

I have a string that contains some numeric data. I want to isolate
these data using re.match, as follows.

bogus = "IFC(35m)"
data = re.match(r'(\d+)',bogus)
print data.group(1)

I would expect to have "35" printed out to screen, but instead I get
an error that the regular expression did not match:

Traceback (most recent call last):
File "C:\Documents and Settings\Mattia\Desktop\Neeltje\read.py",
line 20, in <module>
print data.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Note that the same holds if I look for "35" straight, instead of "\d
+". If instead I look for "IFC" it works fine. That is, apparently
re.match will match only up to the first non-alphanumeric character
and ignore anything after a "(", "_", "[" and god knows what else.

I am using Python 2.6 (r26:66721, latest stable version). Am I missing
something very big and very important?

Yep - re.search. Match matches the whole string. You want searching.


Diez
 
D

Diez B. Roggisch

John said:
*ONLY* if the pattern ends with "$" or r"\Z"


You think so?

import re

rex = re.compile("abc.*def")

if rex.match("abc0123455678def"):
print "matched"



Diez
 
S

Steve Holden

Diez said:
You think so?

import re

rex = re.compile("abc.*def")

if rex.match("abc0123455678def"):
print "matched"
Your test is inconclusive: necessary, but not sufficient.
.... print "Matched"
....
Matched
regards
Steve
 
J

John Machin

You think so?

import re

rex = re.compile("abc.*def")

if rex.match("abc0123455678def"):
     print "matched"

OK, I'll try again:

The following 3-tuples represent (pattern, string,
matched_portion_of_string):
('abc', 'abc', 'abc')
('abc', 'abcdef', 'abc')
('abc$', 'abc', 'abc')
('abc$', 'abcdef', '<no match>')

Saying "Match matches the whole string" is incorrect; see the second
case. If you want to ensure that the whole string matches the pattern,
the pattern needs to be terminated by "$" or "\Z".
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top