我有一个正则表达式'(\<link [^\>]+\>)'
,它匹配得很好,但是在前面添加了一个空格,但是它没有:' (\<link [^\>]+\>)'
(没有'
)。
当我使用正则表达式来匹配一个link
标记时,它完全匹配:
$ echo '<para>Please see Listing <link href="#lst:11-alg1">2.1</link> with the' \
| perl -pe 's|(\<link [^\>]+\>)|###|g'
<para>Please see Listing ###2.1</link> with the
如您所见,<link...>
被###
替换--因此正则表达式匹配。
当我在正则表达式之前添加一个空格时,它不再匹配任何
$ echo '<para>Please see Listing <link href="#lst:11-alg1">2.1</link> with the' \
| perl -pe 's| (\<link [^\>]+\>)|###|g'
<para>Please see Listing <link href="#lst:11-alg1">2.1</link> with the
结果仍然是原始文本,没有替换发生-- regexp没有匹配。
我认为问题是贪婪的匹配(但我无法想象在哪里),我玩了+?
-modifiers和\s
,但没有效果。而且也不是regexp以' '
开头:当我将Listing
添加到模式中时,它也不匹配:
$ echo '<para>Please see Listing <link href="#lst:11-alg1">2.1</link> with the' \
| perl -pe 's|Listing (\<link [^\>]+\>)|###|g'
<para>Please see Listing <link href="#lst:11-alg1">2.1</link> with the
我甚至用Python:相同的结果做了同样的尝试,所以我的regexp肯定有问题。
发布于 2014-08-25 12:17:31
如果您将字符串复制到Notepad++中,这是一个特殊的字符,在清单ANSI(-欧元‡)之后。所以空间无法与之相匹配。
https://stackoverflow.com/questions/25493601
复制相似问题