Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use regex to match characters between a keyword and a delimiting character: Python?

I am trying to learn regex, I am stuck in this problem where I have to match all characters between a keyword and a delimiting character.

For e.g.

in_str="hello world, pincode 80 21 61 (just an example)"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if the keyword is ‘pincode’ and the delimiting character is ‘(‘ , the output has to be 80 21 61 because this lies between the keyword and the character.

Edit:
This is the logic I have tried so far,
m = re.search(r"(?<=pincode)(.*)(?=()", in_str) where I am matching the keyword and the delimiter, but it does not seem to work (I am getting no match)

Any help to solve this will be much appreciated!

>Solution :

import re

str = "hello world, pincode 80 21 61 (just an example)"
result = re.search('pincode(.*)\(', str)
print(result.group(1))

I suggest that do not copy and paste this code, go online and learn this method and learn how it works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading