什么正则表达式将匹配除逗号“,"或分号“;"之外的每个字符? [英] 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.
推荐答案
[^,;]+
您尚未指定正在使用的正则表达式实现.他们中的大多数都有一个 Split
方法,该方法采用分隔符并由它们拆分.您可能希望将那个与普通"(没有 ^
)字符类一起使用:
You haven't specified the regex implementation you are using. Most of them have a Split
method that takes delimiters and split by them. You might want to use that one with a "normal" (without ^
) character class:
[,;]+
这篇关于什么正则表达式将匹配除逗号“,"或分号“;"之外的每个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!