Regex Perl正则表达式匹配

Regex Perl正则表达式匹配,regex,perl,unit-testing,Regex,Perl,Unit Testing,我只想匹配像“abcunit”、“hedhdunit”、“ewedfunit”这样的词,并使用正则表达式从文本文件进入数组。有谁能告诉我怎么才能把那个词排成一行吗。因此,我使用/(..单位)/g,但“单位”之前的字符是不同的。谁能告诉perfect regex这个吗。任何帮助都将不胜感激 单位:阿布库尼特 单位:HEDHDUNT 单位:ewedfant 单位:eedunit您可以尝试以下正则表达式: \b[a-z]+unit\b NODE EXPLANA

我只想匹配像“abcunit”、“hedhdunit”、“ewedfunit”这样的词,并使用正则表达式从文本文件进入数组。有谁能告诉我怎么才能把那个词排成一行吗。因此,我使用/(..单位)/g,但“单位”之前的字符是不同的。谁能告诉perfect regex这个吗。任何帮助都将不胜感激

单位:阿布库尼特

单位:HEDHDUNT

单位:ewedfant


单位:eedunit

您可以尝试以下正则表达式:

\b[a-z]+unit\b
NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [a-z]+                   any character of: 'a' to 'z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  unit                     'unit'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

正则表达式的解释:

\b[a-z]+unit\b
NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  [a-z]+                   any character of: 'a' to 'z' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  unit                     'unit'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

以下内容应满足您的需要

my @matches = $str =~ /\b\w+unit\b/g;

你的输入和预期输出是什么?你好,阿维纳什,我的预期输出是一行一行的数组中的abcunit,hedhdunit ewedfunt eedunit。你的数据实际上是什么样子的?unit:abcunit unit:hedhdunit unit:ewedfunt:eedunit Hey Paul。这两个词都匹配,因为两个词都有逗号单位。我怎么能只听到第二个字呢。我知道了,谢谢,保罗,不客气。不要忘记,如果一个答案令人满意地解决了你的问题,你可以。;-)我会很高兴这样做,当我将有15+的声誉。