Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError Regex
#1
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'
Reply
#2
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?
Reply
#3
(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
Reply
#4
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())
Reply
#5
(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
Reply
#6
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020