我正在尝试匹配可能包含单引号字符(')的输入字符串。我的挑战是,我需要忽略目标字符前面的任意偶数个引号字符,因为它们被认为是转义字符。
以下是我的想法。
(?=('')*)'然而,这还不能达到目的。例如,如果我的输入是''',则正则表达式将匹配所有三个单引号字符,而不仅仅是最后一个。
这里有一些示例。
' ## match
'' ## no-match
''' ## matches the last quote character
'''' ## no-match
abc' ## matches the last quote character
Mike''s home' ## matches the last quote character only任何帮助都将不胜感激。谢谢!
发布于 2014-02-26 00:53:25
我不知道你使用什么环境来测试正则表达式,但是下面的正则表达式是PCRE兼容的,按照你给出的例子工作:
(?<!')(?:'')*\K'(?!')发布于 2014-02-25 23:12:54
基本上,您似乎希望对至少包含一个奇数个单引号字符序列的输入进行检测。
下面是一个正则表达式,我相信它可以满足这个要求:
(^'|[^']')('')*([^']|$)或者,简单地添加?:来抑制捕获组:
(?:^'|[^']')(?:'')*(?:[^']|$)我已经编写了一个Perl程序来根据您提供的示例数据测试这个正则表达式。(我还添加了一些额外的样本输入。)请参阅以下内容,了解程序和程序本身的预期输出。
预期输出:
* [']
* [x']
[x'']
* [x''']
['']
* [''x']
[''x'']
[''x''y]
* [''']
['''']
[''''x]
* [abc ']
* [Mike''s home']
[Mike''s home'']
* [Mike''s home''']
* [Mike''s home'''x]
[Mike''s home'''']
[Mike''s home''''x]演示RegEx的Perl程序:
#/usr/bin/perl
use strict;
use warnings;
while (<DATA>) {
chomp;
my $match = " ";
if (/(^'|[^']')('')*([^']|$)/) {
# ^^ ^^^^^ ^^^^^ ^^^^ ^
# (1a) (1b) (2) 3a 3b
#
# Match the line if:
# (1a) The line begins with a single quote character
# -or-
# (1b) Somewhere contains a non-quote character followed by a single
# quote character
# (2) That is optionally followed by an even number of quote characters.
# (3a) And that is followed by a non-quote character
# -or-
# (3b) The end of the line.
$match = "* "
}
print "$match\[$_\]\n";
}
__END__
'
x'
x''
x'''
''
''x'
''x''
''x''y
'''
''''
''''x
abc '
Mike''s home'
Mike''s home''
Mike''s home'''
Mike''s home'''x
Mike''s home''''
Mike''s home''''x发布于 2014-02-25 21:31:50
https://stackoverflow.com/questions/22015636
复制相似问题