如何将变量与Perl正则表达式的其余部分分开?

时间:2022-09-09 07:32:44

I have a regex where the variable $m_strFirstName is next to other identifier characters that aren't part of the variable name:

我有一个正则表达式,其中变量$ m_strFirstName位于不属于变量名称的其他标识符字符旁边:

if($strWholeName =~ m/$m_strFirstName_(+)/) 
....

I'm trying to extract something like:

我试图提取类似的东西:

  • strWholeName is 'bob_jones' or 'bob_smith'
  • strWholeName是'bob_jones'或'bob_smith'

  • m_strFirstName is 'bob'
  • m_strFirstName是'bob'

  • and I want the 'smith' or 'jones' part of the string.
  • 我想要字符串中的'smith'或'jones'部分。

  • if strWholeName is "frank_jones" I want to ignore that, so the if statement would be false
  • 如果strWholeName是“frank_jones”我想忽略它,所以if语句将是false

Obviously,

m/$m_strFirstName_(+)/

is not going to work because the regex interpreter won't treat the $m_strname part as I intended, so any ideas?

不会起作用,因为正则表达式解释器不会按照我的意图处理$ m_strname部分,所以任何想法?

EDIT: my original question was not clear, updated.

编辑:我原来的问题不明确,更新。

Thanks

4 个解决方案

#1


Put braces around your variable name:

在变量名称周围加上大括号:

if($strname =~ m/${m_strName}_(.+)/)

#2


Josh has the right answer, but if you turned on strict and warnings, you could have found it for yourself. Here's (basically) your script as it was originally:

Josh有正确的答案,但如果你打开严格和警告,你可以自己找到它。这里(基本上)是你最初的脚本:

my($strWholeName) = "Bob_Jones";
my($m_strFirstName) = "Bob";

if($strWholeName =~ m/$m_strFirstName_(.+)/) {
    print "Last name is <$1>\n";
}

which resulted in the following:

结果如下:

C:\temp>perl test.pl
Last name is <Bob_Jones>

If you add the following two lines:

如果添加以下两行:

use strict;
use warnings;

you get the following output instead:

你得到以下输出:

C:\temp>perl test.pl
Global symbol "$m_strFirstName_" requires explicit package name at test.pl line
7.
Execution of test.pl aborted due to compilation errors.

Add in the braces per Josh's answer and you finally get:

根据Josh的回答添加括号,最后得到:

C:\temp>perl test.pl
Last name is <Jones>

Always, always, always use strict and use warnings!

始终,始终,始终使用严格和使用警告!

See brian's Guide to Solving Any Perl Problem from Mastering Perl for more nifty tricks.

有关更多精彩技巧,请参阅brian的“掌握Perl解决任何Perl问题的指南”。

#3


if ($strWholeName =~ m/^\Q$m_strFirstName\E_(.+)/)

Differences from Josh Kelley's answer:

与Josh Kelley的回答不同:

  1. You must anchor the regex to the start of the string, otherwise e.g. a $strWholeName of "jeanne_smith" will incorrectly match a $m_strFirstName of "anne".
  2. 您必须将正则表达式锚定到字符串的开头,否则例如$ strWholeName的“jeanne_smith”将错误地匹配$ m_strFirstName的“anne”。

  3. You should surround $m_strFirstName with \Q and \E to quote any strange characters (unless you are absolutely sure they cannot appear -- but why not do it anyway as it is cheap and guarantees safety). Hynek Vychodil mentioned this in a comment on Josh's answer.
  4. 您应该使用\ Q和\ E包围$ m_strFirstName以引用任何奇怪的字符(除非您绝对确定它们不会出现 - 但为什么不这样做,因为它便宜且保证安全)。 Hynek Vychodil在评论Josh的回答中提到了这一点。

#4


$foo = "yodeling yoda"
$bar = "ing yo"

if ($foo =~ /\Q$bar\E/)
{
  print "true"
}

#1


Put braces around your variable name:

在变量名称周围加上大括号:

if($strname =~ m/${m_strName}_(.+)/)

#2


Josh has the right answer, but if you turned on strict and warnings, you could have found it for yourself. Here's (basically) your script as it was originally:

Josh有正确的答案,但如果你打开严格和警告,你可以自己找到它。这里(基本上)是你最初的脚本:

my($strWholeName) = "Bob_Jones";
my($m_strFirstName) = "Bob";

if($strWholeName =~ m/$m_strFirstName_(.+)/) {
    print "Last name is <$1>\n";
}

which resulted in the following:

结果如下:

C:\temp>perl test.pl
Last name is <Bob_Jones>

If you add the following two lines:

如果添加以下两行:

use strict;
use warnings;

you get the following output instead:

你得到以下输出:

C:\temp>perl test.pl
Global symbol "$m_strFirstName_" requires explicit package name at test.pl line
7.
Execution of test.pl aborted due to compilation errors.

Add in the braces per Josh's answer and you finally get:

根据Josh的回答添加括号,最后得到:

C:\temp>perl test.pl
Last name is <Jones>

Always, always, always use strict and use warnings!

始终,始终,始终使用严格和使用警告!

See brian's Guide to Solving Any Perl Problem from Mastering Perl for more nifty tricks.

有关更多精彩技巧,请参阅brian的“掌握Perl解决任何Perl问题的指南”。

#3


if ($strWholeName =~ m/^\Q$m_strFirstName\E_(.+)/)

Differences from Josh Kelley's answer:

与Josh Kelley的回答不同:

  1. You must anchor the regex to the start of the string, otherwise e.g. a $strWholeName of "jeanne_smith" will incorrectly match a $m_strFirstName of "anne".
  2. 您必须将正则表达式锚定到字符串的开头,否则例如$ strWholeName的“jeanne_smith”将错误地匹配$ m_strFirstName的“anne”。

  3. You should surround $m_strFirstName with \Q and \E to quote any strange characters (unless you are absolutely sure they cannot appear -- but why not do it anyway as it is cheap and guarantees safety). Hynek Vychodil mentioned this in a comment on Josh's answer.
  4. 您应该使用\ Q和\ E包围$ m_strFirstName以引用任何奇怪的字符(除非您绝对确定它们不会出现 - 但为什么不这样做,因为它便宜且保证安全)。 Hynek Vychodil在评论Josh的回答中提到了这一点。

#4


$foo = "yodeling yoda"
$bar = "ing yo"

if ($foo =~ /\Q$bar\E/)
{
  print "true"
}