匹配空格但不匹配换行符 [英] Match whitespace but not newlines
问题描述
有时我想匹配空格而不是换行符.
I sometimes want to match whitespace but not newline.
到目前为止,我一直在使用 [ ]
.有什么不那么尴尬的方式吗?
So far I've been resorting to [ ]
. 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+00A0NO-BREAK SPACE 和 v
也匹配 U+0085 NEXT LINE,但都没有被 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屋!