Java 如何使用正则表达式匹配制表符和换行符而不是空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21620329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:41:55  来源:igfitidea点击:

How to match tab and newline but not space with REGEX?

javaregex

提问by Akshay Lokur

I am trying to match "tab" and "newline" meta chars but without "spaces" with REGEX in Java.

我正在尝试将“制表符”和“换行符”元字符与 Java 中的 REGEX 匹配,但没有“空格”。

\smatches evrything i.e. tab, space and new line... But, I don't want "space" to be matched.

\s匹配所有内容,即制表符、空格和换行符...但是,我不希望匹配“空格”。

How do I do that?

我怎么做?

Thanks.

谢谢。

采纳答案by Rohit Jain

One way to do it is:

一种方法是:

[^\S ]

The negated character class makes this regex to match anything except - \\S(non-whitespace) and " "(space) character. So, it will match \\sexcept space.

否定字符类使此正则表达式匹配除 - \\S(非空白)和" "(空格)字符之外的任何内容。所以,它将匹配\\s除了空间。

回答by falsetru

Explicitly list them inside [...](set of characters):

[...](字符集)中明确列出它们:

"[\t\n\r\f\v]"