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

Time:03-24

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)"

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!

CodePudding user response:

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.

  • Related