Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 正则表达式中是否有类似于计数器变量的内容?_Regex_Language Agnostic - Fatal编程技术网

Regex 正则表达式中是否有类似于计数器变量的内容?

Regex 正则表达式中是否有类似于计数器变量的内容?,regex,language-agnostic,Regex,Language Agnostic,如果我有很多匹配项,例如在多行模式下,我想用部分匹配项以及递增的计数器编号替换它们 我想知道是否有任何regex风格有这样一个变量。我找不到,但我似乎记得有这样的东西存在 我不是说可以使用回调进行替换的脚本语言。它是关于能够在诸如RegexBuddy、sublime text、gskinner.com/RegExr等工具中实现这一点的。。。同样,您可以使用\1或$1引用捕获的子字符串。FMTEYEWTK关于奇特的正则表达式 好的,我要从简单到崇高。享受吧 简单s///e解 鉴于此: #!/usr

如果我有很多匹配项,例如在多行模式下,我想用部分匹配项以及递增的计数器编号替换它们

我想知道是否有任何regex风格有这样一个变量。我找不到,但我似乎记得有这样的东西存在

我不是说可以使用回调进行替换的脚本语言。它是关于能够在诸如RegexBuddy、sublime text、gskinner.com/RegExr等工具中实现这一点的。。。同样,您可以使用\1或$1引用捕获的子字符串。

FMTEYEWTK关于奇特的正则表达式 好的,我要从简单到崇高。享受吧

简单s///e解 鉴于此:

#!/usr/bin/perl

$_ = <<"End_of_G&S";
    This particularly rapid,
        unintelligible patter
    isn't generally heard,
        and if it is it doesn't matter!
End_of_G&S

my $count = 0;
产生这个

(This)[1] (particularly)[2] (rapid)[3],
    (unintelligible)[4] (patter)[5]
(isn't)[6] (generally)[7] (heard)[8], 
    (and)[9] (if)[10] (it)[11] (is)[12] (it)[13] (doesn't)[14] (matter)[15]!
Anon阵列解中的内插码 鉴于:

s/\b([\w']+)\b/#@{[++$count]}=$1/g;
产生以下结果:

#1=This #2=particularly #3=rapid,
    #4=unintelligible #5=patter
#6=isn't #7=generally #8=heard, 
    #9=and #10=if #11=it #12=is #13=it #14=doesn't #15=matter!
(’Tis)[1] (Renée’s)[2] (great‐grandparents’)[3] (summer‐house)[4], (isn’t)[5] (it)[6]?
使用LHS而不是RHS中的代码解决方案 这会将增量放在匹配本身中:

s/ \b ( [\w']+ ) \b (?{ $count++ }) /#$count=$1/gx;
结果如下:

#1=This #2=particularly #3=rapid,
    #4=unintelligible #5=patter
#6=isn't #7=generally #8=heard, 
    #9=and #10=if #11=it #12=is #13=it #14=doesn't #15=matter!
口吃的解决办法 这个

产生了这个令人愉快的答案:

This particularly particularly rapid rapid rapid,
    unintelligible unintelligible unintelligible unintelligible patter patter patter patter patter
isn't isn't isn't isn't isn't isn't generally generally generally generally generally generally generally heard heard heard heard heard heard heard heard, 
    and and and and and and and and and if if if if if if if if if if it it it it it it it it it it it is is is is is is is is is is is is it it it it it it it it it it it it it doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't doesn't matter matter matter matter matter matter matter matter matter matter matter matter matter matter matter!
探索边界 对于复数所有格,还有更为强大的词边界方法(以前的方法没有),但我怀疑你的秘密在于触发
+$count
,而不是
\b
行为的微妙之处

我真的希望人们明白,
\b
不是他们想象的那样。 他们总是认为这意味着有空白或字符串的边缘 那里他们从不认为这是
\w\w
\w\w
转换

# same as using a \b before:
(?(?=\w) (?<!\w)  | (?<!\W) )

# same as using a \b after:
(?(?<=\w) (?!\w)  | (?!\W)  )
哪个正确打印

('Tis)[1] (Paul's)[2] (parents')[3] (summer-house)[4], (isn't)[5] (it)[6]?
担心Unicode 20世纪60年代风格的ASCII大约已经过时50年了。正如每当你看到有人写
[a-z]
时,几乎总是错的,结果是破折号和引号之类的东西也不应该在模式中显示为文本。当我们使用它时,您可能不想使用
\w
,因为它还包括数字和下划线,而不仅仅是字母

想象一下这个字符串:

$_ = qq(\x{2019}Tis Ren\x{E9}e\x{2019}s great\x{2010}grandparents\x{2019} summer\x{2010}house, isn\x{2019}t it?\n);
您可以使用utf8将其作为文本使用

use utf8;
$_ = qq(’Tis Renée’s great‐grandparents’ summer‐house, isn’t it?\n);
这一次,我将以稍微不同的方式来处理该模式,将术语的定义与其执行分离开来,以使其更具可读性和可维护性:

#!/usr/bin/perl -l
use 5.10.0;
use utf8;
use open qw< :std :utf8 >;
use strict;
use warnings qw< FATAL all >;
use autodie;

$_ = q(’Tis Renée’s great‐grandparents’ summer‐house, isn’t it?);

my $count = 0;

s{ (?<WORD> (?&full_word)  )

   # the rest is just definition
   (?(DEFINE)

     (?<word_char>   [\p{Alphabetic}\p{Quotation_Mark}] )

     (?<full_word>

             # next line won't compile cause
             # fears variable-width lookbehind
             ####  (?<! (?&word_char) )   )
             # so must inline it

         (?<! [\p{Alphabetic}\p{Quotation_Mark}] )

         (?&word_char)
         (?:
             \p{Dash}
           | (?&word_char)
         ) *

         (?!  (?&word_char) )
     )

   )   # end DEFINE declaration block

}{
    sprintf "(%s)[%d]", $+{WORD}, ++$count;
}gsex;

print;

好吧,这可能是关于花式正则表达式的FMTEYEWT,但是你不高兴你问了吗?☺

有些语言允许调用指定的函数,例如JavaScript:
var i=0;“foobar”.replace(/o/g,function(match){return match+”(“+(i+++)”);})
。那么您使用的是什么语言呢?我使用诸如或regexbuddy之类的工具来简化对代码块的手动编辑,因此在这类工具中工作的东西是最好的。如果OP假设所有regex风格都相同,那么它的语言是不可知的,或者计数器是一个常见的功能。事实并非如此。此外,回调并不是味道的一部分,它只是一个奇特的迭代器。不管怎样,你都应该发布你正在使用的语言,也许有一个聪明的两步解决方案。如果你要求一些可能的目标语言,你可能会得到更多的解决方案。另一方面,你也可能会错过一些有趣的解决方案,因为你可能会被一个非常小的最大公因数所困扰。谢谢你的广泛研究。如果我打算使用perl,我肯定会对此进行彻底的研究。我很有信心任何脚本语言都能做到这一点,因此,如果我最终为此编写了一个脚本,我可能会坚持使用python,因为我已经了解python了。@ufotds:在python中不能做这些事情,因为python不支持Unicode属性,也不支持定义块,也不支持使用命名缓冲区作为子例程。如果您使用的是Unicode文本和正则表达式,那么如果您既不使用Perl也不使用real PCRE,就必须做出严重的妥协。请参阅和,了解您在Unicode属性支持方面缺少的内容。
use utf8;
$_ = qq(’Tis Renée’s great‐grandparents’ summer‐house, isn’t it?\n);
#!/usr/bin/perl -l
use 5.10.0;
use utf8;
use open qw< :std :utf8 >;
use strict;
use warnings qw< FATAL all >;
use autodie;

$_ = q(’Tis Renée’s great‐grandparents’ summer‐house, isn’t it?);

my $count = 0;

s{ (?<WORD> (?&full_word)  )

   # the rest is just definition
   (?(DEFINE)

     (?<word_char>   [\p{Alphabetic}\p{Quotation_Mark}] )

     (?<full_word>

             # next line won't compile cause
             # fears variable-width lookbehind
             ####  (?<! (?&word_char) )   )
             # so must inline it

         (?<! [\p{Alphabetic}\p{Quotation_Mark}] )

         (?&word_char)
         (?:
             \p{Dash}
           | (?&word_char)
         ) *

         (?!  (?&word_char) )
     )

   )   # end DEFINE declaration block

}{
    sprintf "(%s)[%d]", $+{WORD}, ++$count;
}gsex;

print;
(’Tis)[1] (Renée’s)[2] (great‐grandparents’)[3] (summer‐house)[4], (isn’t)[5] (it)[6]?