简体   繁体   English

Perl字符串包含括号

[英]Perl string contains bracket

I have a question. 我有个问题。 I have a variable that contains brackets for example: 我有一个包含方括号的变量,例如:

Local $string = "abc[0].xzy"

Now I want to search for all strings in a file that matches that string $string . 现在,我想搜索文件中与该字符串$string匹配的所有$string The problem is that the string contains a bracket and I can't change the to abc\\[0\\]\\.xzy . 问题是该字符串包含方括号,并且我无法将其更改为abc\\[0\\]\\.xzy Is there a solution to solve my problem? 有解决我的问题的解决方案吗?

You may use \\Q \\E block (quotemeta) which treats all the chars present inside the block as literal chars. 您可以使用\\Q \\E块(quotemeta),它将块中存在的所有字符视为文字字符。

/\Q$string\E/

Reference 参考

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

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