[Tutor] Regular Expression help

Kent Johnson kent37 at tds.net
Wed Jun 27 18:22:36 CEST 2007


Tom Tucker wrote:
> #matchstr regex flow
> # (\(\d+,\d+\))     # (0018,0014)
> # \s                   # [space]
> # (..*)                # Contrast/Bolus Administration Route Sequence
> # \s                   # space
> # ([a-z]{2})         # SQ - two letters and no more
> # \s                  # [space]
> # (\d)                # 1 - single digit
> # re.I)               # case insensitive
> 
> matchstr = re.compile(r"(\(\d+,\d+\))\s(..*)\s([a-z]{2})\s(\d)",re.I)

You should learn about re.VERBOSE:
http://docs.python.org/lib/node46.html#l2h-414

With this flag, your commented version could be the actual regex, 
instead of repeating it in code with the whitespace and comments removed:

matchstr = re.compile(r'''
(\(\d+,\d+\))        # (0018,0014)
\s                   # [space]
(..*)                # Contrast/Bolus Administration Route Sequence
\s                   # space
([a-z]{2})           # SQ - two letters and no more
\s                  # [space]
(\d)                # 1 - single digit
re.I)               # case insensitive
''', re.I|re.VERBOSE)

Kent


More information about the Tutor mailing list