简体   繁体   中英

Allow conditional usage of semicolon in regex pattern

I have the following pattern:

UnallowedCharacters = @"<>\{\}" + "\"";
@"^(?<contactType>\d+):(?<contactIdentifier>[^;" + UnallowedCharacters + @"]+)(;(?<parameterName>[A-Za-z0-9_-]+)=(?<parameterValue>[^;=" + UnallowedCharacters + "]+))*$"

I need to allow the usage of semicolon in the contactIdentifier part, but still to not exclude the semicolon from not allowed chars, because the later split will not work anymore.

Any ideas?

If I have understood the question, you can do this:

UnallowedCharacters = @"<>{}"""; (no need to escape inside a character group)

(?<contactIdentifier>(?:[;]|[^" + UnallowedCharacters + @"])+

Explanation :

I changed the <contactIdentifier> group to:

?<contactIdentifier> the name

(?: start of (non capturing) group

[;]| ';' OR:

[^" + UnallowedCharacters + @"] one character not in class

)+ The whole group repeated one or more times.

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