Regular expression: find spaces (tabs/space) but not newlines

空扰寡人 提交于 2019-11-27 11:02:19

问题


How can I have a regular expression that tests for spaces or tabs but not newlines. I tried \s but found out that it tests for newlines too.

I use C#/WPF but it shouldn't matter.


回答1:


Use character classes: [ \t]




回答2:


Try this character set:

[ \t]

This does only match a space or a tabulator.




回答3:


As @Eiríkr Útlendi noted, the accepted solution only considers two white space characters: the horizontal tab (U+0009), and a breaking space (U+0020). It does not consider other whitespace characters such as non-breaking spaces (which happen to be in the text I am trying to deal with). A more complete whitespace character listing is included on Wikipedia and also referenced in the linked Perl answer. A simple C# solution that accounts for these other characters can be built using character class subtraction

[\s-[\r\n]]

or, including Eiríkr Útlendi's solution, you get

[\s\u3000-[\r\n]]



回答4:


Note: For those dealing with CJK text (Chinese, Japanese, and Korean), the double-byte space (Unicode \u3000) is not included in \s for any implementation I've tried so far (Perl, .NET, PCRE, Python). You'll need to either normalize your strings first (such as by replacing all \u3000 with \u0020), or you'll have to use a character set that includes this codepoint in addition to whatever other whitespace you're targeting, such as [ \t\u3000].

If you're using Perl or PCRE, you have the option of using the \h shorthand for horizontal whitespace, which appears to include the single-byte space, double-byte space, and tab, among others. See the Match whitespace but not newlines (Perl) thread for more detail.

However, this \h shorthand has not been implemented for .NET and C#, as best I've been able to tell.




回答5:


If you want to replace space below code worked for me in C#

Regex.Replace(Line,"\\\s","");

For Tab

Regex.Replace(Line,"\\\s\\\s","");



来源:https://stackoverflow.com/questions/3583111/regular-expression-find-spaces-tabs-space-but-not-newlines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!