Regular Expression_How to extract several matching patterns from a line?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Regular Expression_How to extract several matching patterns from a line?



I have a .csv document consists of several lines. In each line I have tab separated informations such as,


name_1:ayse t name_2:fatma t birth_date_1:24 t birth_date_2:august t birth_date_3:2018 t death_date:2100 t location:turkey.



The sequence of these informations may not be same in each line and there many informations like this in each line.



What am I trying to do is to get a specific part of the string which only has "birth_date" information in it.



I am managed to get only all 3 strings related with birth date as follows


['birth_date_1', 'birth_date_2', 'birth_date_3']



with the help of below code.


inputfile = open('ornek_data.csv','r',encoding="utf-8")

for rownum, line in enumerate(inputfile):
pattern_birth = re.compile(r"w*birth_datew*",re.IGNORECASE)
if pattern_birth.search(line) is not None:
a = re.findall("w*birth_datew*", line)
print(a)



However what i want actually is to extact below list as output and write it in another document for each line.


['birth_date_1:24', 'birth_date_2:august', 'birth_date_3:2018']



I tried several other regular expressions methods such as below but I couldn't handle it. I will be glad if anyone can help me with this problem.


for rownum, line in enumerate(inputfile):
pattern_birth = re.compile(r"w*birth_datew*",re.IGNORECASE)
if pattern_birth.search(line) is not None:
a = re.findall("w*birth_date.*?:$", line)
print(a)




3 Answers
3



I would not use a regex here.


regex



Split on 't' and check if the splitted contains 'birth_date', simple!:


't'


'birth_date'


s = 'name_1:ayse t name_2:fatma t birth_date_1:24 t birth_date_2:august t birth_date_3:2018 t death_date:2100 t location:turkey.'

print([x.strip() for x in s.split('t') if 'birth_date' in x])
# ['birth_date_1:24', 'birth_date_2:august', 'birth_date_3:2018']



Use "w*birth_date.*?s" or r"birth_date_d:.*?s"


"w*birth_date.*?s"


r"birth_date_d:.*?s"



Ex:


import re

line = "name_1:ayse t name_2:fatma t birth_date_1:24 t birth_date_2:august t birth_date_3:2018 t death_date:2100 t location:turkey."
print(re.findall("w*birth_date.*?s", line))



Output:


['birth_date_1:24 ', 'birth_date_2:august ', 'birth_date_3:2018 ']





thank you very much. it worked.
– Kaan Karabal
12 mins ago





You are welcome.
– Rakesh
3 mins ago



Your regex doesn't match what you are trying to extract, so you need to extend it.



As an aside, you should only re.compile once - the point of compilation is to avoid needing to parse the regex again.


re.compile



There is also no need to check for no matches separately. Just loop over all the matches; if there are none, the loop will execute zero times.


pat = re.compile(r"bbirth_date_d+:d+",re.IGNORECASE)

with open('ornek_data.csv','r',encoding="utf-8") as inputfile:
for rownum, line in enumerate(inputfile):
for a in pat.findall(line):
print(rownum, a)



The w* wasn't doing anything useful (if you don't care if it's there or not, as the * quantifier does, why search for it?) whereas b requires the match to occur at a word boundary (so adjacent to whitespace or punctuation, or beginning or end of line). d matches a digit and : simply matches itself.


w*


*


b


d


:



If this is a well-formed CSV file, maybe instead use a CSV reader and print the fields which match startswith('birth_date_')


startswith('birth_date_')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived