Why is the regular expression returning an error in python?

Am trying the following regular expression in python but it returns an error

import re
...

#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...

m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)

i get the following error:

AttributeError: 'NoneType' object has no attribute 'group'


Asked by: Melissa230 | Posted: 06-12-2021






Answer 1

This is happening because the regular expression wasn't matched. Therefore m is None and of course you can't access group[0]. You need to first test that the search was successful, before trying to access group members.

Answered by: Carlos996 | Posted: 07-01-2022



Answer 2

Two issues:

  1. your pattern does not match, therefore m is set to None, and None has no group attribute.

  2. I believe you meant either:

    m= re.search(r"(?<=WORD)\w+", str(line))
    

    as entered, or

    m= re.search(r"(?P<WORD>\w+)", str(line))
    

    The former matches "abc" in "WORDabc def"; the latter matches "abc" in "abc def" and the match object will have a .group("WORD") containing "abc". (Using r"" strings is generally a good idea when specifying regular expressions.)

Answered by: Daisy882 | Posted: 07-01-2022



Answer 3

re.search documentation says:

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.

Answered by: Aston692 | Posted: 07-01-2022



Answer 4

>>> help(re.search)
Help on function search in module re:

search(pattern, string, flags=0)
    Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found.

Look at the last line: returning None if no match was found

Answered by: Sarah532 | Posted: 07-01-2022



Answer 5

BTW, your regular expression is incorrect. If you look for 'WORD', it should be just 'WORD'. str is also extraneous. Your code should look like this:

m = re.search('WORD', line)
if m:
    print m.group[0]

Your original regexp will return probably unexpected and undesired results:

>>> m = re.search('(?<=[WORD])\w+', 'WORDnet')
>>> m.group(0)
'ORDnet'

Answered by: Paul623 | Posted: 07-01-2022



Similar questions

regex - Python Regular Expression returning nothing to repeat?

So, what I'm trying to do is to parse a text file line by line into a list. I've successfully done that. Now, I need to pull all the links that end with .html out. Therefore I thought I would parse each line and if it matched *.html. So I believe the best way to do this is regular expressions. Below is my code and the error in question is the error returned about nothing to repeate...


regex - Python regular expression not returning desired results

I have a string like this {test}{test2}{test3} i have a function with regex that is supposed to extract everything between the {} def cleanup(): mystring = str('{test}{test2}{test3}') rx = re.findall(r'{(.*)}', mystring) for row in rx: return(rx) but when I print this function instead of getting the desired test, test2, ...


regex - Python regular expression not returning all groups

I have a string like this: &lt;hello&lt;world&lt;1 \&lt; 2&gt;, which represents a list of three strings "hello", "world", "1 &lt; 2". I want my regular expression to be able to match ("hello", "world", "1 \&lt; 2"). (I will remove the backslashes later in evaluation). I'm using the following regular expression to match the text: r"(?:&lt;((?:[^&lt;&gt;]|\\.)*)...


python - Why is this regular expression not returning anything?

I've recently been working on a project in the field of machine learning, and I'm having some trouble with a regular expression that is needed for my data. For some reason, instead of returning a decimal value as it should, it ends up returning a value of None. Any suggestions? Below is my code, and the file I'm trying to get data out of. for list_num in range(0,92): mach = [3,4,5,6,7] for j in rang...


regex - I want to use regular expression for returning index in python

I want to get index from array. For example: s = "arr[2]" I want to use regular expression for returning index[2] Updated! I want also to return the name of array (arr)


python - Regular Expression not returning results

I've written a regular expression and tested it in regex101.com, yet when I implement it in my code, I get no values returned and I have got no idea why. I'm scraping a HTML document (an RSS feed specifically) and have got other regex's working with that HTML document within the same program, just not this particular one! I'm just at a loss since it works in regex101.com (and in another Python program I have acces...


python - Expression returning syntax error

I was auditing some code at a local real estate company that has a severely outdated and hacked together system. Anyways I stumbled upon this error while trying to fix a very strange convoluted tag/comment sorting algorithm. I can't quite parse the code: &gt;&gt;&gt;''.join(re.findall("@(.{1})",'@'.join map(lambda x: x[::-1],['where are you a@', 'cruise queen mary on', 'morgtage ninjitsu', 'maritimes', 'pro...


parsing - Returning a function from a user supplied expression in Python

So I'm working on writing a program that will need to take an expression as user input, and evaluate that expression as values change over in-program time. There will be many of these expressions. There will be a "play" function to have time move forward, so I'd like to keep things fast(ish) which usually means "find something somebody else wrote", but I'm certainly not against writing my own stuff. I'm expecting ...


python - returning string matching regular expression

I have a textfile with lines of which I want to extract one line that matches a certain pattern. Can I search for that line with a regular expression and return it, to work with it afterwards? Is there a more elegant way than for lines in file: if re.match(targetregex,line)!=None: print line Something like: print re.matchingline(targetregex,file.read()) ...


regex - Python Regular Expression returning nothing to repeat?

So, what I'm trying to do is to parse a text file line by line into a list. I've successfully done that. Now, I need to pull all the links that end with .html out. Therefore I thought I would parse each line and if it matched *.html. So I believe the best way to do this is regular expressions. Below is my code and the error in question is the error returned about nothing to repeate...


regex - Python regular expression not returning desired results

I have a string like this {test}{test2}{test3} i have a function with regex that is supposed to extract everything between the {} def cleanup(): mystring = str('{test}{test2}{test3}') rx = re.findall(r'{(.*)}', mystring) for row in rx: return(rx) but when I print this function instead of getting the desired test, test2, ...


python - Is it possible to use Regular Expression to alter a string directly instead of returning altered version of the string?

I'm trying to directly edit an XML file's text. I'd prefer to find and remove a certain phrase potentially by using the "sub" function. For particular reasons I'd prefer not to return the edited strings and then find a way to replace the existing XML file test. Is there an easy way to do this? Thanks for any help.


regex - Python Regular expression not returning as expected

I am having trouble understanding the output of this regular expression. I am using the following regex to find a dates in text: ^(?:(1[0-2]|0?[1-9])-(3[01]|[12][0-9]|0?[1-9])|(3[01]|[12][0-9]|0?[1-9])-(1[0-2]|0?[1-9]))-(?:[0-9]{2})?[0-9]{2}$ It appears to be matching the pattern within text correctly, but I'm confused by the return values. For this test string: Te...


regex - Regular expression returning None (python)

I'm only a month into python and this basic exercise is driving me up the wall. I'm trying to make a search script that will search text I input and put the results on my clipboard. I've been stuck below for about a week. If i copy text directly from a site and input it, no results (I get a None output). But if i copy in the number directly, no problems it reads them perfectly. I've tried it several ways (shown below) and ...


python - Why does returning this lambda expression result in a string?

I recently started learning Python 3.5.1, and am currently experimenting with lambda expressions. I tried setting up the simple method below. def sum_double(a, b): return lambda a, b: a+b if a != b else (a+b)*2, a, b All it is supposed to do is return the sum of a and b, and twice their sum if a is equal to b, but instead I...


regex - Python. Regular expression not returning output

I am trying to findall instances of the string "PB" and the digits that follow it, but when I call. number_all = re.findall(r'\bPB\b([0-9])\d+', ' '.join(number_list)) the ([0-9])\d+ doesn't return an output. I check my output file, sequence.txt but there is nothing inside it. If i just do \bPB\b it outputs "PB"...


python - Regular expression to match start of filename and filename extension

What is the regular expression to match strings (in this case, file names) that start with 'Run' and have a filename extension of '.py'? The regular expression should match any of the following: RunFoo.py RunBar.py Run42.py It should not match: myRunFoo.py RunBar.py1 Run42.txt The SQL equivalent of what I am looking for is ... LIKE 'Run%.py' ...


regex - python regular expression to split paragraphs

How would one write a regular expression to use in python to split paragraphs? A paragraph is defined by 2 linebreaks (\n). But one can have any amount of spaces/tabs together with the line breaks, and it still should be considered as a paragraph. I am using python so the solution can use python's regular expression syntax whi...


python - Problem with Boolean Expression with a string value from a lIst

I have the following problem: # line is a line from a file that contains ["baa","beee","0"] line = TcsLine.split(",") NumPFCs = eval(line[2]) if NumPFCs==0: print line I want to print all the lines from the file if the second position of the list has a value == 0. I print the lines but after that the following happens: Traceback (most recent call last): ['baaa'...


python - split twice in the same expression?

Imagine I have the following: inFile = "/adda/adas/sdas/hello.txt" # that instruction give me hello.txt Name = inFile.name.split("/") [-1] # that one give me the name I want - just hello Name1 = Name.split(".") [0] Is there any chance to simplify that doing the same job in just one expression?


python - Regular expression to extract URL from an HTML link

I’m a newbie in Python. I’m learning regexes, but I need help here. Here comes the HTML source: &lt;a href="http://www.ptop.se" target="_blank"&gt;http://www.ptop.se&lt;/a&gt; I’m trying to code a tool that only prints out http://ptop.se. Can you help me please?


regex - How can I translate the following filename to a regular expression in Python?

I am battling regular expressions now as I type. I would like to determine a pattern for the following example file: b410cv11_test.ext. I want to be able to do a search for files that match the pattern of the example file aforementioned. Where do I start (so lost and confused) and what is the best way of arriving at a solution that best matches the file pattern? Thanks in advance.


python - What is the regular expression for the "root" of a website in django?

I'm using django and when users go to www.website.com/ I want to point them to the index view. Right now I'm doing this: (r'^$', 'ideas.idea.views.index'), However, it's not working. I'm assuming my regular expression is wrong. Can anyone help me out? I've looked at python regular expressions but they didn't help me.


regex - Python Regular Expression to add links to urls

I'm trying to make a regular expression that will correctly capture URLs, including ones that are wrapped in parenthesis as in (http://example.com) and spoken about on coding horror at https://blog.codinghorror.com/the-problem-with-urls/ I'm currently using the foll...


python - Regular expression to detect semi-colon terminated C++ for & while loops

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this: for (int i = 0; i &lt; 10; i++); ... but not this: for (int i = 0; i &lt; 10; i++) This looks trivial at first glance, until you realise...


regex - How do i write a regular expression for the following pattern in python?

How do i look for the following pattern using regular expression in python? for the two cases Am looking for str2 after the "=" sign Case 1: str1=str2 Case 2: str1 = str2 please note there can be a space or none between the either side of the "=" sign Mine is like this, but only works for one of the cases! m=re...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top