如何使用REGEX匹配制表符和换行符而不是空格?

时间:2023-01-27 23:39:47

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

我试图匹配“tab”和“newline”元字符,但没有“空格”与Java中的REGEX。

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

\ s匹配evrything,即tab,space和new line ......但是,我不希望“space”匹配。

How do I do that?

我怎么做?

Thanks.

谢谢。

2 个解决方案

#1


19  

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 \\s except space.

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

#2


2  

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

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

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

#1


19  

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 \\s except space.

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

#2


2  

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

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

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