Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a Regex
#1
Hi,

I have this code:
import re

text = 'NoErrorRE: [EXTERNAL] PL_192_01_28Claire Gravell KingService EscalationNormal'

p = re.compile(r'^NoError.*\s*$')


matches = p.finditer(text)
for match in matches:
    print(match)
What I'm trying to do is to get, from a string, a sentence that start's with 'NoError', then anything after it, but I want everything to the end of the sentence.

This is what I get in my output:
Output:
<re.Match object; span=(0, 79), match='NoErrorRE: [EXTERNAL] PL_192_01_28Claire Gravell >
'Gravell KingService EscalationNormal' is ignored from the ouput, how do I fix my regex to get the full text back?
Reply
#2
Try match.group(). That should have the full text matched. The str() of a match object (what is printed) doesn't necessarily show the whole match found.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Like this should do it,if want also NoErrorRE: just group().
import re

text = '''\
abc 13456
NoErrorRE: [EXTERNAL] PL_192_01_28Claire Gravell KingService EscalationNormal
No car 99999 [EXTERNAL]
NoErrorRE: [EXTERNAL] PL_999_1111_80Claire Gravell KingService EscalationNormal'''

r = re.compile(r'NoErrorRE:\s(.*)')
for match in r.finditer(text):
    print(match.group(1))
Output:
[EXTERNAL] PL_192_01_28Claire Gravell KingService EscalationNormal [EXTERNAL] PL_999_1111_80Claire Gravell KingService EscalationNormal
This could be done without regex to.
text = '''\
abc 13456
NoErrorRE: [EXTERNAL] PL_192_01_28Claire Gravell KingService EscalationNormal
No car 99999 [EXTERNAL]
NoErrorRE: [EXTERNAL] PL_999_1111_80Claire Gravell KingService EscalationNormal'''

for line in text.split('\n'):
    if line.strip().startswith('NoErrorRE:'):
        print(line)
        #--| Uncomment for no "NoErrorRE:"   
        #_, line = line.split('NoErrorRE:')
        #print(line.strip())
Output:
NoErrorRE: [EXTERNAL] PL_192_01_28Claire Gravell KingService EscalationNormal NoErrorRE: [EXTERNAL] PL_999_1111_80Claire Gravell KingService EscalationNormal
Reply
#4
Thank you for the response, I actually managed to get it working, this is what I did:
pattern = re.compile(r'NoError.*')
found = re.findall(pattern, text)

for match in found:
    print(match)
Reply
#5
When iterating over lines is better to use re.finditer().
So this generate first a list found = re.findall(pattern, text)
Then iterating over that list(found),of course not problem if input is small.

Here loop directly,so now only line for line is in memory.
pattern = re.compile(r'NoError.*')
for match in pattern.finditer(text):
    print(match.group())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading a Regex pattern stahorse 12 5,223 Apr-25-2019, 10:21 AM
Last Post: NewBeie

Forum Jump:

User Panel Messages

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