简体   繁体   English

在Perl中==是否等效于Python中的re.match?

[英]Is =~ in Perl equivalent to re.match in Python?

I'm trying to replicate Perl Fathom in Python. 我正在尝试在Python中复制Perl Fathom。 This step corrects the syllable count for some word combinations. 此步骤纠正某些单词组合的音节计数。 Is =~ equivalent to re.match in Python? =〜是否等效于Python中的re.match? re.match only looks for the first instance. re.match仅查找第一个实例。 Thank you! 谢谢!

 @SubSyl = (
           'cial',
           'tia',
           'cius',
           'cious',
           'giu',              
           'ion',
           'iou',
           'sia$',
           '.ely$',             
           '[^td]ed$',          
          );

 foreach (@SubSyl) {
          $syl-- if $word =~ /$_/;
        }

=~ is the binding operator. =~是绑定运算符。 It can bind a match, substitution, or transliteration. 它可以绑定匹配,替换或音译。 I guess only the first one could be equivalent to matching in Python, but it seems you need re.search instead, as matching with // is not anchored at the beginning of the string. 我猜只有第一个可以等同于Python中的匹配,但是似乎您需要re.search ,因为与//匹配未锚定在字符串的开头。

Certainly not re.match() . 当然不是re.match() It's a bit of a gotcha, but re.match matches pattern only starting at the beginning of the string. 这有点re.match ,但是re.match匹配模式仅在字符串的开头开始。

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. 如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象。 Return None if the string does not match the pattern; 如果字符串与模式不匹配,则返回None;否则返回false。 note that this is different from a zero-length match. 请注意,这与零长度匹配不同。

re.search() is more likely what you are looking for (unless you regex patter happens to always match beginning of the string). re.search()更有可能是您要寻找的内容(除非您的正则表达式模式总是与字符串的开头匹配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM