简体   繁体   中英

python regex include end-of-line char in capture group or not?

When doing a search within a line that includes the end of the line should one include the $ into the capture group or not?

Example included:

x=re.search("\S+(bla\S+bla$)",line)

Example excluded:

x=re.search("\S+(bla\S+bla)$",line)

Are there significant advantages / risks of one over the other?

Thanks.

The $ ending anchor is zero-width, meaning that even if you do capture it, it won't contribute anything to the capture group. So, I might actually consider the first version to be an anti-pattern, and would therefore always use the second version:

x = re.search("\S+(bla\S+bla)$", line)

In both cases, the underlying pattern is the same, so I would expect the regex engine to take the same steps, and have the same performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM