Skip to content

Instantly share code, notes, and snippets.

@parker
Created January 21, 2012 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parker/1654495 to your computer and use it in GitHub Desktop.
Save parker/1654495 to your computer and use it in GitHub Desktop.
Regular expression challenge
Does anyone have a regular expression that would pull out all of the arguments as groups?
Example inputs (1 per line):
"arg one", "arg two", "arg three"
"arg one", arg.two, arg.three
"arg one", arg.two, "arg.three"
"arg , one", arg.two, "arg.three"
"class=\"arg\" one", arg.two, "arg.three"
"class=\"arg\" one", "arg , two", "arg.three"
"class=\"arg\" one", "arg \" \" two", "arg.three"
"arguments: A, B, \"C\", D", "arg \" \" two", "arg.three"
The groups for the last input would be an array of length 3.
@mdirolf
Copy link

mdirolf commented Jan 22, 2012

This uses backtracking and hence generates some extra groups. But works (at least on your inputs).

import re

inputs = ['"arg one", "arg two", "arg three"',
          '"arg one", arg.two, arg.three',
          '"arg one", arg.two, "arg.three"',
          '"arg , one", arg.two, "arg.three"',
          '"class=\\"arg\\" one", arg.two, "arg.three"',
          '"class=\\"arg\\" one", "arg , two", "arg.three"',
          '"class=\\"arg\\" one", "arg \\" \\" two", "arg.three"',
          '"arguments: A, B, \\"C\\", D", "arg \\" \\" two", "arg.three"']


for input in inputs:
    match = re.match(r'("?)(.*?[^\\])\1, ("?)(.*?[^\\])\3, ("?)(.*?[^\\])\5$', input)
    print '%s : %s : %s' % (match.group(2), match.group(4), match.group(6))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment