请解释这两行来自this code的Perl .
# separate out "," except if within numbers (5,300)
# previous "global" application skips some: A,B,C,D,E > A , B,C , D,E
# first application uses up B so rule can't see B,C
# two-step version here may create extra spaces but these are removed later
# will also space digit,letter or letter,digit forms (redundant with next section)
$text =~ s/([^\p{IsN}])[,]/$1 , /g;
$text =~ s/[,]([^\p{IsN}])/ , $1/g;
最佳答案
您的代码进行了两次替换。 s///
是一个替换,/g
标志告诉它全局,这本质上是替换所有.
这两行都使用了 \p{}
unicode property character groups在正则表达式中。使用 IsN
,它检查字符是否为数字。
/
( # capture group
[ # character group
^ # not the following characters
\p{IsN} # all characters that are a number
]
)
[,] # followed by a comma
/
它用捕获的非数字、一个空格、一个逗号和一个空格替换它。
第二行做同样的事情,但逗号在前。
您可以重写此代码,使其更短、更简洁且具有相同的功能。 \P{}
是所有不具有此属性的字符,这消除了对 []
的需要。这可以进一步缩短为 \PN
。
$text =~ s/(\PN),/$1 , /g;
$text =~ s/,(\PN)/ , $1/g;
关于regex - 用 Perl 标记逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48080038/