Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 关于Perl正则表达式的查询_Regex_Perl - Fatal编程技术网

Regex 关于Perl正则表达式的查询

Regex 关于Perl正则表达式的查询,regex,perl,Regex,Perl,这个问题和我昨天问的一个有关。我是Perl新手,仍然掌握着一些诀窍*。在代码中,我试图用撇号替换右单引号。然而,我不想在单引号的单词上替换正确的单引号。例如: He said the movie was 'magnificent.' 以下是我目前使用的代码: #!/usr/bin/perl use strict; use warnings; # Subroutine prototype sub problem_character(); my $previousPosition=0; my

这个问题和我昨天问的一个有关。我是Perl新手,仍然掌握着一些诀窍*。在代码中,我试图用撇号替换右单引号。然而,我不想在单引号的单词上替换正确的单引号。例如:

He said the movie was 'magnificent.' 
以下是我目前使用的代码:

#!/usr/bin/perl
use strict;
use warnings;

# Subroutine prototype
sub problem_character();

my $previousPosition=0;
my $currentPosition=0;

#Locates problematic apostrophes and replaces them with properly encoded apostrophes
sub problem_character(){
    while($_[0]=~m/\x{2019}/g){
        $currentPosition=pos($_[0]);
        pos($_[0])=$previousPosition;
        unless(....){
            $_[0]=~s/\x{2019}/\x{0027}/g;
        }
        $previousPosition=$currentPosition;
    }
}
首先,我不确定我会在支票上写什么。我希望能够检查匹配的正确单引号是否是单引号单词的一部分。另外,在Perl文档中,pos函数是最后一次
m//q
搜索停止的偏移量。替换搜索是否也属于此类别?最后,有没有更简单的方法来编写这种类型的代码?谢谢


*有谁知道我能找到一本详细解释危险的好书吗?我发现在线资源非常混乱。

您发布了以下内容:

He said the movie was 'magnificent.'
但是您说您试图替换该字符串中不存在的
。你真的有以下几点吗

He said the movie was ‘magnificent.’
如果是这样,简单的解决方案是替换所有与前面的
不匹配的
。不过,实现起来有点棘手

s{
    \G
    (?: [^\x{2018}\x{2019}]++
    |   \x{2018} [^\x{2018}\x{2019}]*+ \x{2019}?+
    )*+
    \K
    \x{2019}
}{'}xg;

更简单(但效率稍低)的实现:

$_ = reverse($_);
s/\x{2019}(?![^\x{2018}\x{2019}]*\x{2018})/'/g;
$_ = reverse($_);

顺便说一下,如果需要,您可以在正则表达式模式中实际使用字符
'
'
。只需确保使用UTF-8对文件进行编码,并告诉Perl您使用的是
使用utf8

use utf8;  # Source code is encoded using UTF-8.

$_ = reverse($_);
s/’(?![^‘’]*‘)/'/g;
$_ = reverse($_);

我使用了Perl文档、StackOverflow和这一个教程:我想我很快就学会了。Perl编程是我从20年前学到的。Pro提示:不要使用子程序原型——它们不会做你在其他语言中习惯的事情,也很少有用。放弃任何鼓励您使用Perl原型的在线资源。除了上面提到的书之外,这是非常好的。是的,谢谢。这正是我正在寻找的。