Home > front end >  Python Regular Expression for String Matching
Python Regular Expression for String Matching

Time:01-04

Which regular expression can I use to match the strings of pattern- XX-YYY-XXXXXZ, where X is any digit, YYY is any alphabet pattern needed to be matched, Z is the alphabet.

for eg. if three strings are 89-ABC-98765Z, 76-GHI-67453H, 76-ABC-76453A I need the output strings with "ABC" i.e 89-ABC-98765Z, 76-ABC-76453A

Trying to use str.match(r'.[0-9][ABC][0-9][A-Z]?'). P.S I am trying to use it in a dataframe column.

CodePudding user response:

import re

pattern = r'\d -[A-Z] -\d [A-Z]'
text = 'for eg. if three strings are 89-ABC-98765Z, 76-GHI-67453H, 76-ABC-76453A I need the output strings with "ABC" i.e 89-ABC-98765Z, 76-ABC-76453A'
res = re.findall(pattern,text)
print(res)

CodePudding user response:

The regex should be:

\d{2}-ABC-\d{5}[A-Z]

Explanation:

\d{2}   # 2 digits
-ABC-   # literal "-ABC-"
\d{5}   # 5 digits
[A-Z]   # any uppercase letter
  •  Tags:  
  • Related