Python Forum
Regex on more than one line ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex on more than one line ?
#1
I have a regular expression that is long how do I organize the regex by skipping lines just so it doesn't get too long, ex :

var = re.match(r"my.................................................................................regex too long", string)
how can i continue writing the regex by skipping line ?
Reply
#2
Try implicit string concatenation:
var = re.match(
  r"my.."     # comment here
  r"........" # comment here
  r"........"
  r"...............................................................regex too long",
  string)
Reply
#3
There is what re.VERBOSE is made for splitting up long regex and eg adding comments.
import re

email = re.compile(r"""
    [\w.]+        # match any alphanumeric character or a period
    @             # match the character literally
    \w+\.[a-z]{3} # Any character followed by period and exactly three lower-case letters""", re.VERBOSE)
print(email.findall('[email protected] [email protected]')
Output:
['[email protected]']
The same as.
import re

email = re.compile(r'[\w.]+@\w+\.[a-z]{3}')
print(email.findall('[email protected] [email protected]'))
Output:
['[email protected]']
Reply
#4
(Mar-11-2020, 11:51 PM)micseydel Wrote: Try implicit string concatenation:
var = re.match(
  r"my.."     # comment here
  r"........" # comment here
  r"........"
  r"...............................................................regex too long",
  string)
Thank.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  regex multi-line kucingkembar 6 1,441 Aug-27-2022, 10:27 PM
Last Post: kucingkembar
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,407 Aug-22-2021, 06:59 PM
Last Post: Winfried
  Regex won't replace character with line break Tomf96 2 2,500 Jan-12-2020, 12:14 PM
Last Post: Tomf96

Forum Jump:

User Panel Messages

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