如何使用perl在正则表达式中包含一个变量作为模式匹配的一部分?

时间:2021-03-15 21:40:02

I have a variable that stores the device name say $dev_to_connect = "XYZ keyboard". I want it to include it in my regular expression as part of pattern matching. I have tried using \Q..\E. But i found it not helpful.

我有一个变量,它存储设备名$dev_to_connect = "XYZ键盘"。我希望它包含在我的正则表达式中,作为模式匹配的一部分。我试过用\Q. \E。但我发现这没有帮助。

The regular expression i am using is 'Dev:(\d)\r\n\tBdaddr:(..):(..):(..):(..):(..):(..)\r\n\tName:\Q$device_to_connect\E'

我使用的正则表达式是“Dev:(\ d)\ r \ n \ tBdaddr:(. .):(. .):(. .):(. .):(. .):(. .)\ r \ n \ tName:\问device_to_connect美元\ E”

I want the \Q$device_to_connect\E part of regular expression to be matched with the original value in the variable.

我想让\Q$device_to_connect\E部分正则表达式与变量中的原始值相匹配。

3 个解决方案

#1


3  

Single quotes don't interpolate. You could use double-quotes, but that would require a lot of escaping. qr// is designed for this very purpose.

单引号不插入。你可以使用双引号,但那需要很多转义。qr//是为这个目的而设计的。

qr/Dev:(\d)...Name:\Q$device_to_connect\E/

#2


0  

Suppose you have to find double words in a document, this is how to do it:

假设你必须在一份文件中找到两个单词,以下是如何做到的:

\b(\w+)\s+\1\b

\ b(\ w +)\ s + 1 \ \ b

And here is the anatomy:

解剖结构是这样的:

<!--
\b(\w+)\s+\1\b

Options: ^ and $ match at line breaks

Assert position at a word boundary «\b»
Match the regular expression below and capture its match into backreference number 1 «(\w+)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the same text as most recently matched by capturing group number 1 «\1»
Assert position at a word boundary «\b»
-->

Calling the group number is only the way to call/include a preceding group in pattern. Hope this halp. Visit here for reference.

调用组号只是在模式中调用/包含前面的组的方法。希望这能为。访问这里供参考。

#3


0  

I think you have your variable names mixed up. You define $dev_to_connect but you reference $device_to_connect in your regex. If you fix that using a variable in a regex is straightforward:

我觉得你把变量名搞混了。您定义了$dev_to_connect,但是您在regex中引用了$device_to_connect。如果您在regex中使用一个变量进行修复,那么很简单:

$var = 'foo';
if ($_ =~ /$var/) {
  print "Got '$var'!\n";
}

Here is a snippet out of one of my scripts that works:

下面是我的一个脚本的片段:

if ($ctlpt =~ /$owner/) {
  ($opt_i) && print "$prog: INFO: $psd is on $ctlpt.\n";
} else {
  print "$prog: WARNING: $psd is on $ctlpt, and not on $owner.\n";
}

#1


3  

Single quotes don't interpolate. You could use double-quotes, but that would require a lot of escaping. qr// is designed for this very purpose.

单引号不插入。你可以使用双引号,但那需要很多转义。qr//是为这个目的而设计的。

qr/Dev:(\d)...Name:\Q$device_to_connect\E/

#2


0  

Suppose you have to find double words in a document, this is how to do it:

假设你必须在一份文件中找到两个单词,以下是如何做到的:

\b(\w+)\s+\1\b

\ b(\ w +)\ s + 1 \ \ b

And here is the anatomy:

解剖结构是这样的:

<!--
\b(\w+)\s+\1\b

Options: ^ and $ match at line breaks

Assert position at a word boundary «\b»
Match the regular expression below and capture its match into backreference number 1 «(\w+)»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the same text as most recently matched by capturing group number 1 «\1»
Assert position at a word boundary «\b»
-->

Calling the group number is only the way to call/include a preceding group in pattern. Hope this halp. Visit here for reference.

调用组号只是在模式中调用/包含前面的组的方法。希望这能为。访问这里供参考。

#3


0  

I think you have your variable names mixed up. You define $dev_to_connect but you reference $device_to_connect in your regex. If you fix that using a variable in a regex is straightforward:

我觉得你把变量名搞混了。您定义了$dev_to_connect,但是您在regex中引用了$device_to_connect。如果您在regex中使用一个变量进行修复,那么很简单:

$var = 'foo';
if ($_ =~ /$var/) {
  print "Got '$var'!\n";
}

Here is a snippet out of one of my scripts that works:

下面是我的一个脚本的片段:

if ($ctlpt =~ /$owner/) {
  ($opt_i) && print "$prog: INFO: $psd is on $ctlpt.\n";
} else {
  print "$prog: WARNING: $psd is on $ctlpt, and not on $owner.\n";
}