regex - How to retrieve substring form a string using regular expression in python -


i have data of form

cs989_-red814298959 cs663_red812657324 red819238322_cs537 ...... 

this data in csv file. want retrieve sub strings starting red. please suggest me way using regular expression in python

i tried following code:

import re string="red819238322_cs537" substring=re.match("[a-za-z]*//([0-9]*)",string) 

it's returning none

help on function match in module re:

match(pattern, string, flags=0) try apply pattern at start of string, returning match object, or none if no match found.

you want re.search or re.findall instead. regexp incorrect - if want "red" followed number of digits, it's spelled r"red[0-9]+"

>>> strings ['cs989_-red814298959', 'cs663_red812657324', 'red819238322_cs537'] >>> re.match(r"(red[0-9]+)", strings[0]) >>> re.findall(r"(red[0-9]+)", strings[0]) ['red814298959'] >>> re.findall(r"(red[0-9]+)", strings[1]) ['red812657324'] >>> re.findall(r"(red[0-9]+)", strings[2]) ['red819238322'] >>> re.search(r"(red[0-9]+)", strings[0]) <_sre.sre_match object @ 0x1772e40> 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -