Posts: 9
Threads: 5
Joined: Jun 2019
hello everybody, i'm learning python, use re module, and i had a error, help me please.
import re
numRegex=re.compile(r"^\d{1,3}(,(\d{3}))*$")
mo = numRegex.search(" 12,124,123,221 ")
print(mo.group()) => AttributeError: 'NoneType' object has no attribute 'group'
Posts: 360
Threads: 5
Joined: Jun 2019
Quote:re.search(pattern, string, flags=0)
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
So the question is: if you print(mo) what value is shown?
Posts: 9
Threads: 5
Joined: Jun 2019
(Jun-22-2019, 01:26 PM)ThomasL Wrote: Quote:re.search(pattern, string, flags=0)
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
So the question is: if you print(mo) what value is shown?
it show "None", i think it don't recognize the string or my compile() has mistake
Posts: 7,153
Threads: 122
Joined: Sep 2016
Jun-22-2019, 02:22 PM
(This post was last modified: Jun-22-2019, 02:22 PM by snippsat.)
Your string start with one white-space and end with two,when using ^ and $ it most match the begin/end of string exactly on each line.
Could be written like this numRegex = re.compile(r"^\s\d{1,3}(,(\d{3})).*$")
Or if strip of white-space it will also work.
import re
numRegex=re.compile(r"^\d{1,3}(,(\d{3}))*$")
mo = numRegex.search(" 12,124,123,221 ".strip())
print(mo.group())
Posts: 9
Threads: 5
Joined: Jun 2019
(Jun-22-2019, 02:22 PM)snippsat Wrote: Your string start with one white-space and end with two,when using ^ and $ it most match the begin/end of string exactly on each line.
Could be written like this numRegex = re.compile(r"^\s\d{1,3}(,(\d{3})).*$")
Or if strip of white-space it will also work.
import re
numRegex=re.compile(r"^\d{1,3}(,(\d{3}))*$")
mo = numRegex.search(" 12,124,123,221 ".strip())
print(mo.group())
oh, yeah, i don't know it before. tks verymuch. So it doesn't seem very convenient
Posts: 2,038
Threads: 9
Joined: May 2017
Jun-23-2019, 09:01 PM
(This post was last modified: Jun-23-2019, 09:01 PM by DeaD_EyE.)
This is a case, where a function returns an object with methods or None .
Usually you have to check if there is a result instead of direct accessing an attribute/method.
import re
num_reg = re.compile(r'something')
text = 'something else'
match = num_reg.search(text)
if match:
print(match.group())
else:
print('No match') The upcoming 3.8 version of Python introduced the walrus operator (assignment expression):
import re
num_reg = re.compile(r'something')
text = 'something else'
if match := num_reg.search(text):
print(match.group())
else:
print('No match') https://medium.com/hultner/try-out-walru...30ce0ce601
|