Python Forum
Exception handling in regex using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Exception handling in regex using python (/thread-26488.html)



Exception handling in regex using python - ShruthiLS - May-03-2020

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


RE: Exception handling in regex using python - anbu23 - May-04-2020

Do you want to match lines without "VAR1"? then try (VAR1)*