Python Forum
Exception handling in regex using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception handling in regex using python
#1
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
Reply
#2
Do you want to match lines without "VAR1"? then try (VAR1)*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question log.exception() without arguments in old Python versions? cthart 5 2,456 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
Star python exception handling handling .... with traceback mg24 3 3,676 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Python Regex quest 2 4,502 Sep-22-2022, 03:15 AM
Last Post: quest
  Handling pdf files with python. fuzzin 1 1,945 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 3,942 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Handling Python Fatal Error richajain1785 7 9,442 Oct-14-2021, 01:34 PM
Last Post: Tails86
  python regex: get rid of double dot wardancer84 4 3,403 Sep-09-2021, 03:03 PM
Last Post: wardancer84
  Handling multi-input/output audio in python bor1904 4 5,145 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Python Requests package: Handling xml response soumyarani 1 3,130 Sep-14-2020, 11:41 AM
Last Post: buran
  Handling Multiple USB ports in Python samalpramod 0 6,409 Aug-01-2020, 07:40 PM
Last Post: samalpramod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020