perl正则表达式在摩西的两个数字之间加点的区别

Difference between perl regex to add dots between two numbers in Moses

上下文,我正在尝试将 Perl 代码从 https://github.com/moses-smt/mosesdecoder/blob/master/scripts/tokenizer/normalize-punctuation.perl#L87 移植到 Python,Perl 中有这个正则表达式:

s/(\d) (\d)/./g;

如果我在给定输入文本 123 45 的情况下使用 Perl 脚本尝试它,它 returns 是带点的相同字符串。作为完整性检查,我也在命令行上尝试过:

echo "123 45" | perl -pe 's/(\d) (\d)/./g;' 

[输出]:

123.45

当我将正则表达式转换为 Python、

时它也会这样做
>>> import re
>>> r, s = r'(\d) (\d)', '\g<1>.\g<2>'
>>> print(re.sub(r, s, '123 45'))
123.45

但是当我使用摩西文字时:

$ wget https://raw.githubusercontent.com/moses-smt/mosesdecoder/master/scripts/tokenizer/normalize-punctuation.perl
--2019-03-19 12:33:09--  https://raw.githubusercontent.com/moses-smt/mosesdecoder/master/scripts/tokenizer/normalize-punctuation.perl
Resolving raw.githubusercontent.com... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 905 [text/plain]
Saving to: 'normalize-punctuation.perl'

normalize-punctuation.perl    100%[================================================>]     905  --.-KB/s    in 0s      

2019-03-19 12:33:09 (8.72 MB/s) - 'normalize-punctuation.perl' saved [1912]

$ echo "123 45" > foobar

$ perl normalize-punctuation.perl < foobar
123 45

即使我们尝试打印 Moses code 中正则表达式前后的字符串,即

if ($language eq "de" || $language eq "es" || $language eq "cz" || $language eq "cs" || $language eq "fr") {
    s/(\d) (\d)/,/g;
    }
else {
    print $_;
    s/(\d) (\d)/./g;
    print $_;
    }

[输出]:

123 45
123 45
123 45

我们看到在正则表达式前后,字符串没有变化。

我的部分问题是:

这个来自 moose 的代码不起作用的原因是它搜索不间断的 space,而不仅仅是 space。很难看出来,但是 hexdump 可以帮助你:

fe-laptop-p:moose fe$ head -n87 normalize-punctuation.perl | tail -n1 | hexdump -C
00000000  09 73 2f 28 5c 64 29 c2  a0 28 5c 64 29 2f 24 31  |.s/(\d)..(\d)/|
00000010  2e 24 32 2f 67 3b 0a                              |./g;.|
00000017
fe-laptop-p:moose fe$ head -n87 normalize-punctuation.perl.with_space | tail -n1 | hexdump -C
00000000  09 73 2f 28 5c 64 29 20  28 5c 64 29 2f 24 31 2e  |.s/(\d) (\d)/.|
00000010  24 32 2f 67 3b 0a                                 |/g;.|
00000016

看看区别:c2 a0 vs 20?

p.s。 关于在正则表达式中添加加号的评论:这里不需要,因为在两个相邻数字之间放置点号就足够了,不需要找到完整的数字