Skip to main content
simplify regex
Source Link
yuri
  • 4.6k
  • 3
  • 19
  • 40

You could use a different regex:

import re s = r' "C:\Program Files (x86)\myeditor" "$FILEPATH" -n$LINENO "c:\Program Files" -f$FILENAME -aArg2' pattern = re.compile("r"((\".+?\"\"[^\"]+\")|(\-.+?)(?:\s|$[^\s]+))") for m in re.finditer(pattern, s): print(m.group(0)) 

This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).

However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.

You could use a different regex:

import re s = r' "C:\Program Files (x86)\myeditor" "$FILEPATH" -n$LINENO "c:\Program Files" -f$FILENAME -aArg2' pattern = re.compile("((\".+?\")|(\-.+?)(?:\s|$))") for m in re.finditer(pattern, s): print(m.group(0)) 

This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).

However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.

You could use a different regex:

import re s = r' "C:\Program Files (x86)\myeditor" "$FILEPATH" -n$LINENO "c:\Program Files" -f$FILENAME -aArg2' pattern = re.compile(r"((\"[^\"]+\")|(-[^\s]+))") for m in re.finditer(pattern, s): print(m.group(0)) 

This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).

However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.

Source Link
yuri
  • 4.6k
  • 3
  • 19
  • 40

You could use a different regex:

import re s = r' "C:\Program Files (x86)\myeditor" "$FILEPATH" -n$LINENO "c:\Program Files" -f$FILENAME -aArg2' pattern = re.compile("((\".+?\")|(\-.+?)(?:\s|$))") for m in re.finditer(pattern, s): print(m.group(0)) 

This regex will match either an item enclosed by double quotes (") or an item prepended with a dash (-).

However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.