简体   繁体   中英

Python Regular Expression expression excluiding string containing pattern match

I want to create a regular expression to match url links that contain underscore character but I don´t what to match a specific string containing undercore. Bellow are some example with and without matchs indicated:
"site.com/_price_" -> Match
"site.com/price" -> No Match
"site.com/_Desde_" -> No Match - This is important

I have tried the following patterns without success

"\[_](?!\_Desde_)"
"\(?!\_Desde_)\[_]"
"\[\_](^\_Desde\_)"

You may use

_(?!Desde_)[^_]*_

See the regex demo

It matches

  • _ - an underscore
  • (?!Desde_) - a negative lookahead that fails the match if the _ is followed with Desde_ substring
  • [^_]* - 0+ chars other than _
  • _ - an underscore.

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