在正则表达式匹配中检测标量的确切字符串值 [英] Detect exact string value of scalar in regex matching

查看:33
本文介绍了在正则表达式匹配中检测标量的确切字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 $foo = "bar.baz"

我想使用标量 $foo 来查找包含bar.baz"(字符串中的任何位置)的字符串,而不是 $foo 的正则表达式评估版本.

I want to use the scalar $foo to find strings that contain "bar.baz" (anywhere in the string), but not the regex-evaluted version of $foo.

所以这行:if( $other =~ m/$foo/) ...不起作用,因为 $foo 正在被评估,使得 '.'被评估为任何字符.我该如何阻止?

So the line: if( $other =~ m/$foo/ ) ... isn't working, because $foo is being evaluated such that the '.' is evaluated to any character. How do I stop that?

推荐答案

选择一个:

  1. $foo = quotemeta("bar.baz");
  2. if ($other =~ m/\Q$foo/)

(两者实际上是同一件事,只是在不同的时间完成.)

(Both are actually the same thing, just done at different times.)

这篇关于在正则表达式匹配中检测标量的确切字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆