Sublime Forum

How to work with the output of a Reg Exp search

#1
I have the following code set up.

import re

text = '''
Shopping
Maps
More
Settings
Tools
About 402928282828828299191 results (0.17 seconds) 
Google promotion
Try Google Search Console
www.google.com/webmasters/
Search Results
Web results
'''

x = re.search('About ([0-9]\w+) results', text)
print(x)

This is what print(x) prints out...and the large number is what i want to strip out..
i  just want to get the big number following About..I am very familiar with all the string processing like explode, split and grabbing stubstrings. but unless I manually put in in a variable none work on it.. like its not a string yet.
 "<re.Match object; span=(35, 70), match='About 402928282828828299191 results'>"




This is a  second attempt to use pattern match to pull that one line out.. and it worked fine to get the line out again but still would not allow and processing as string until I manually saved it into a variable.

pattern = re.compile(r'About [0-9]\w+ results')
matches = pattern.match(text)
print(matches)
x = print(matches)

This match gives same results above but neither will allow sting handling.. is there a way to get it into a string i tried all I could find like x =str() and x.str() and x.(str(g)

Above is what the print(matches comes out like. I could use match or search mode and both work but since its at the beginning of a line I am using Match. When i try to run any string stuff on it it will not work if I use the x that loaded directly from the program.. but if I take the exact same printed string and put it into any variable then all the text handling functions work fine.
I tried converting x to a string but no help.. but if i manually save it into a file it works fine as a string.
My goal is the grab that long number between About and results.. but nothing is working..
unless I manually save it into a variable by hand..
I have tried everything i can think of and am all ears for any new ideas. Simply makes no sense to me.

So bottom line is this is what pops out into x from either program.. 
 "<re.Match object; span=(35, 70), match='About 402928282828828299191 results'>"
But NO string handling will work while in x.. I had to copy this line into a variable even like x = 
and it is fine now.. but i cannot be there to move the string manually.. something stings here.
0 Likes

#2

this seems like less of an ST question and more of a Python question…
with your first example, you can get the number using print(x.group(1)) - you enclosed the number in a capture group, so that is how to extract it - by referencing that group
You can check it is now a string using print(type(x.group(1)))

0 Likes

#3

Thanks for that… I just finished studying groups so I may be able to pull it out like that…will try…
But digging tonight I realized that what that output is probly and object… so of course objects don’t play well with string handling…and it looks awfully complicated to convert an object to string…so will study the groups to pull it off… Thanks again.

0 Likes

#4

Thank you soooo much kingkeith… using groups solved my problem and was able to divert spending a ton of time trying to convert an object to a string…
I was able to save the number 123456789 direct to a cvs file without a hitch… Learning more daily with the help of all you masters…

data = 'Start a sentence and About 123456789 results then bring it to an end'
pattern = re.compile(r'About [0-9]\w+ results')
matches = pattern.search(data)
print(matches)

# g = "<re.Match object; span=(35, 70), match='About 123456789 results'>"

print(matches.group(0))

# About 123456789 results

pigs = matches.group(0).split(' ')

print(pigs[1])

#123456789

with open('csvfile.csv','w') as file:
        file.write(pigs[1])

I have include my final working code here in case it might help someone in the future.

0 Likes