regex - 用 Perl 标记逗号

标签 regex perl

请解释这两行来自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/

相关文章:

regex - 单引号中的双引号,反之亦然

javascript - 用于替换两个选项之间字符的正则表达式

PHP RegEx 删除字符串中每个逗号后的空格

linux - 如何仅从其他列中查找和排序值

perl - perl 中的对象类名

PHP 到 Perl 套接字通信

java - 去掉 lib-noir 中的尾部斜杠

java - 用管道字符 ("|"分割字符串)

perl - 在 Apache httpd-2.4.2 上安装 mod_perl-2.0.7

linux - 如何将输出格式化为列?