本文翻译自:What regex will match every character except comma ',' or semi-colon ';'?
Is it possible to define a regex which will match every character except a certain defined character or set of characters? 是否可以定义一个正则表达式,该正则表达式将匹配除某个定义的字符或字符集之外的每个字符?
Basically, I wanted to split a string by either comma (,) or semi-colon (;). 基本上,我想用逗号(,)或分号(;)分割字符串。 So I was thinking of doing it with a regex which would match everything until it encountered a comma or a semi-colon. 所以我想用一个正则表达式来完成它,它会匹配所有内容,直到遇到逗号或分号。
#1楼
参考:https://stackoom.com/question/5uaQ/什么正则表达式将匹配除逗号-或分号-之外的每个字符
#2楼
[^,;]+
You haven't specified the regex implementation you are using. 您尚未指定正在使用的正则表达式实现。 Most of them have a Split
method that takes delimiters and split by them. 他们中的大多数都有一个Split
方法,它接受分隔符并由它们分割。 You might want to use that one with a "normal" (without ^
) character class: 您可能希望使用具有“普通”(不带^
)字符类的那个:
[,;]+
#3楼
Use character classes. 使用字符类。 A character class beginning with caret will match anything not in the class. 以插入符号开头的字符类将匹配类中没有的任何内容。
[^,;]
#4楼
用这个:
([^,;]*[,;])*
#5楼
使用负字符类 :
[^,;]+