[Solved] Defining regular expression


I would use findall for this..

>>> import re
>>> s="AN : GSHJ488GL67 Customer : sh3893 Acnt No : cgk379gu Name : xyz"
>>> re.findall(r'\b(?:AN|Acnt No) : (\w+)', s)
['GSHJ488GL67', 'cgk379gu']

Explanation:

\b         # the boundary between a word character and not a word character
(?:        # group, but do not capture:
  AN       #   'AN'
 |         #  OR
  Acnt No  #   'Acnt No'
)          # end of grouping
 :         #   ' : '
(          # group and capture to \1:
  \w+      #   word characters (a-z, A-Z, 0-9, _) (1 or more times)
)          # end of \1

0

solved Defining regular expression