0

Python is about reading the code and understands what is happening. That is what I like about Python. Regex is in general hard to read. codinghorror explains this very well, and also made a suggestion to split up the regular expression in multiple line for readability.

Code

F.e I have the working regex :

vms_list = re.findall(r'"[^"]*"', vms)

I would like to do something like :

vms_list = re.findall(r'
    "     # find token " 
    [^"]  # find any char which is no " token
    *     # repeat this until
    "     # token " is found again
    ', vms)

Solution :

  vms_list = re.findall(r"""   # start
                        ["]     # find token double quote, surround with square brackets
                        [^"]    # find token which is not token double quote
                        *       # repeat this until
                        ["]     # # until token double quote is found again
                        """, vms, re.VERBOSE)  # close 

Research

I have searched for multiline regex, with many response, but not about the readability of regex.
Composed Regular Expressions - breaking a regex down into a readable form from 2009 came close but without a solution.

IgnorePatternWhitespace is advised to used like in codehorror blog. But I can not find it, or something similar, for Python.

Readability

This would really improve the readability of my Python regex. Is something like this possible in Python?

Community
  • 1
  • 1
Bernard
  • 681
  • 1
  • 8
  • 21
  • 2
    Yes, use the [VERBOSE](https://docs.python.org/2/library/re.html#re.VERBOSE) flag. – Ashwini Chaudhary Jul 15 '14 at 09:59
  • @undefined-is-not-a-function Looks very attractive. I tried but it does not work for me. The token " makes this example complex. Could you give an example? – Bernard Jul 15 '14 at 10:16
  • 2
    @Bernard use triple quotes to split for splitting a string to multiple lines. `re.findall(r"""Your mu"l"ti"line regex here""", re.VERBOSE)` – msvalkon Jul 15 '14 at 10:19
  • @msvalkon Thanks I tried but the output of the regex differ from original. What is my mistake? – Bernard Jul 15 '14 at 10:37
  • I used square bracklets and now my regex returns the same result as the original. Thanks for your help "Undefined is not a function" and msvalkon – Bernard Jul 15 '14 at 10:58

0 Answers0