Python Forum

Full Version: Exception handling in regex using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am having a regular expression to be read from the file.

regex = '(START\s*([a-zA-Z]\w*)\s*([a-zA-Z]\w*)\s*([a-zA-Z]\w*)\s*([a-zA-Z]\w*)\s*VAR1(?P<var1>\s*(0|1).[0-9]\d*) VAR2(?P<var2>\s*(0|1).[0-9]\d*) VAR3(?P<var3>\s*(0|1).[0-9]\d*))'

pattern = re.search(regex,line)

How can we add exceptions to VAR1,VAR2,VAR3 if the value doesn't exists. And how to equate the value VAR1,VAR2,VAR3 to None. If the value doesn't exists.

Suggestions are appreciated.

Thank you,
Shruthi LS
Do you want to match lines without "VAR1"? then try (VAR1)*