匹配空格,但不换行 [英] Match whitespace but not newlines
问题描述
我有时想匹配空格而不是换行符.
I sometimes want to match whitespace but not newline.
到目前为止,我一直在求助于[ \t]
.有没有那么尴尬的方法?
So far I've been resorting to [ \t]
. Is there a less awkward way?
推荐答案
Perl版本5.10和更高版本支持辅助的垂直和水平字符类\v
和\h
,以及通用的空白字符类\s
Perl versions 5.10 and later support subsidiary vertical and horizontal character classes, \v
and \h
, as well as the generic whitespace character class \s
最干净的解决方案是使用水平空格字符类\h
.这将匹配ASCII集中的制表符和空格,扩展ASCII或任何这些Unicode字符的不间断空格
The cleanest solution is to use the horizontal whitespace character class \h
. This will match tab and space from the ASCII set, non-breaking space from extended ASCII, or any of these Unicode characters
U+0009 CHARACTER TABULATION
U+0020 SPACE
U+00A0 NO-BREAK SPACE (not matched by \s)
U+1680 OGHAM SPACE MARK
U+2000 EN QUAD
U+2001 EM QUAD
U+2002 EN SPACE
U+2003 EM SPACE
U+2004 THREE-PER-EM SPACE
U+2005 FOUR-PER-EM SPACE
U+2006 SIX-PER-EM SPACE
U+2007 FIGURE SPACE
U+2008 PUNCTUATION SPACE
U+2009 THIN SPACE
U+200A HAIR SPACE
U+202F NARROW NO-BREAK SPACE
U+205F MEDIUM MATHEMATICAL SPACE
U+3000 IDEOGRAPHIC SPACE
垂直空间模式\v
不太有用,但可以匹配这些字符
The vertical space pattern \v
is less useful, but matches these characters
U+000A LINE FEED
U+000B LINE TABULATION
U+000C FORM FEED
U+000D CARRIAGE RETURN
U+0085 NEXT LINE (not matched by \s)
U+2028 LINE SEPARATOR
U+2029 PARAGRAPH SEPARATOR
有七个与\v
匹配的垂直空白字符和十八个与\h
匹配的水平空白字符. \s
匹配二十三个字符
There are seven vertical whitespace characters which match \v
and eighteen horizontal ones which match \h
. \s
matches twenty-three characters
所有空格字符都是垂直或水平,没有重叠,但是它们不是正确的子集,因为\h
还会匹配U + 00A0 NO-BREAK SPACE,并且\v
也匹配U + 0085下一行,\s
All whitespace characters are either vertical or horizontal with no overlap, but they are not proper subsets because \h
also matches U+00A0 NO-BREAK SPACE, and \v
also matches U+0085 NEXT LINE, neither of which are matched by \s
这篇关于匹配空格,但不换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!