简体   繁体   中英

Exclude delimiter from regular expression

I'm pretty new at regular expressions. For a string like this:

[quote="Username;123456]

I have created this regular expression:

%\[quote=("|&\#039;|"|\'|)([^\r\n]*?)[^;](\d+)\]%s

This puts out 3 matches:

  • "
  • Username;
  • 23456

Why is [^;] not negating the semicolon, but instead removing one digit and how can I fix this? Thanks.

Your ([^\\r\\n]*?) can eat the ; , so the [^;] is free to take a digit (because it will match anything except the ; ).

You probably wanted ; (without [^ ] :

%\[quote=("|&\#039;|"|\'|)([^\r\n]*?);(\d+)\]%s

I think this might be closer to what you want:

%\[quote=("|\')([\w]*)(?:;)(\d*)\]%s

正则表达式可视化

Demo

The problem with your original pattern:

[^;] (for example) is telling the engine to match something that isn't a semi-colon, it's not 'negating' it - so it captures the first digit which meets this criteria due to the fact it's not a semi-colon

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