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 1,336 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
Star python exception handling handling .... with traceback mg24 3 1,414 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Python Regex quest 2 2,609 Sep-22-2022, 03:15 AM
Last Post: quest
  Handling pdf files with python. fuzzin 1 1,352 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,571 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Handling Python Fatal Error richajain1785 7 6,295 Oct-14-2021, 01:34 PM
Last Post: Tails86
  python regex: get rid of double dot wardancer84 4 2,498 Sep-09-2021, 03:03 PM
Last Post: wardancer84
  Using Regex Expression With Isin in Python eddywinch82 0 2,376 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  Handling multi-input/output audio in python bor1904 4 3,764 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Python Requests package: Handling xml response soumyarani 1 2,272 Sep-14-2020, 11:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

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