简体   繁体   English

匹配任何不是字母的特殊字符(包括下划线,但不包括空格)

[英]Match any special characters (including underscore, but not space) that are not letters

I want to match any special characters that are not numbers or letters (that people use to write words).我想匹配任何不是数字或字母(人们用来写字)的特殊字符。 I want to include underscore because underscore is neither a number nor a letter that is used in words.我想包括下划线,因为下划线既不是数字也不是单词中使用的字母。 But I do not want to include space.但我不想包括空格。

In short, I want to match everyone below except the last two.简而言之,我想匹配除最后两个之外的每个人。

12345_678
12345*678
12345-678
12345&678
12345-678
12345あ678
12345 678

I could not use [^a-zA-Z0-9] because it does not include non-Latin letters such as Japanese.我不能使用[^a-zA-Z0-9]因为它不包括非拉丁字母,如日语。 \\d+(\\W|_)\\d+ got the unwanted space. \\d+(\\W|_)\\d+得到了不需要的空间。 What would be the best regular expression for this?什么是最好的正则表达式?

使用以下也忽略日语字母:

[^a-zA-Z\d\s぀-ゟ゠-ヿ一-龯]

The following regex will match any character that is neither an alphanumeric character (including characters of different alphabets such as those used in Japan or Korea) nor a space.以下正则表达式将匹配既不是字母数字字符(包括不同字母表的字符,例如在日本或韩国使用的字符)也不是空格的任何字符。

([^\w ]|_)

Note the alteration explicitly matching the underscore character, which is necessary since the underscore is part of the \\w character class and thus would not be matched by [^\\w ] alone.请注意显式匹配下划线字符的更改,这是必要的,因为下划线是 \\w 字符类的一部分,因此不会单独由[^\\w ]匹配。 (Also note that the pattern possesses a space character after \\w) (另请注意,该模式在 \\w 之后有一个空格字符)

If not just simple space characters but any other white-space characters (such as the tab character, for example) should be excluded from the match, too, then the following slightly modified pattern might be more appropriate:如果不只是简单的空格字符而且任何其他空白字符(例如制表符)也应该从匹配中排除,那么以下稍微修改的模式可能更合适:

([^\w\s]|_)


( See here for an example of the latter pattern in action on regexstorm.net, including Hiragana and Hangul characters ) 请参阅此处了解 regexstorm.net 上的后一种模式示例,包括平假名和韩文字符

You may want to look at Unicode Character Categories .您可能需要查看Unicode 字符类别 It seems that you need to match for Symbols and Punctuation .似乎您需要匹配SymbolsPunctuation

var regexPattern = @"[\p{S}\p{P}]";

Symbols include +, -, =, <, $, ^, ¦, § etc符号包括 +、-、=、<、$、^、|、§ 等

Punctuation include _, —, (, {, ", », !, ?, #, * etc标点符号包括 _, —, (, {, ", », !, ?, #, * 等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#将所有特殊字符(包括空格)替换为- - Replace all Special Characters including space with - using C# 我需要验证字符串的第一个字符是否为字母,下一个字符是否为字母或数字或特殊字符 _、-、。 和空间 - I need to validate if first character of a string is a letter and next characters are either letters or numbers or special characters _,-,. and space 正则表达式与数字和特殊字符,但没有字母 - Regex with numbers and special characters but no letters 检查正则表达式是否包含字母数字和下划线字符 - Check regex for letters numbers and underscore characters 替换特殊字符或特殊字符后跟空格 - Replace special characters or special characters followed by space 匹配可选的特殊字符 - match optional special characters 如何使用正则表达式将两个大写字母匹配在一起,前面没有特殊字符? - How can I match two capital letters together, that aren't preceded by special characters, using regex? 正则表达式匹配每个以另一个单词开头的单词(包括特殊字符) - Regular expression to match every word starting with another word (including special characters) 在RichTextBox中显示特殊字符(韩文字母) - Display Special Characters (Korean Letters) in RichTextBox 用于捕获字母之间具有特殊字符的单词的正则表达式 - Regex for catching word with special characters between letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM