简体   繁体   English

Escaping Perl 正则表达式中的特殊字符

[英]Escaping special characters in Perl regex

I'm trying to match a regular expression in Perl. My code looks like the following:我正在尝试匹配 Perl 中的正则表达式。我的代码如下所示:

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ($source =~ m/$pattern/) {
  print "Match found!"
}

The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing.问题出现在当 Perl 试图匹配正则表达式时,括号指示字符 class(或者我读到的),并且匹配最终失败。 I know that I can escape the brackets with \[ or \] , but that would require another block of code to go through the string and search for the brackets.我知道我可以使用\[\]转义括号,但这需要通过字符串将另一个代码块添加到 go 并搜索括号。 Is there a way to have the brackets automatically ignored without escaping them individually?有没有办法让括号自动忽略而无需单独使用 escaping?

Quick note: I can't just add the backslash, as this is just an example.快速说明:我不能只添加反斜杠,因为这只是一个示例。 In my real code, $source and $pattern are both coming from outside the Perl code (either URIEncoded or from a file).在我的真实代码中, $source$pattern都来自 Perl 代码之外(URIEncoded 或文件)。

\\Q will disable metacharacters until \\E is found or the end of the pattern. \\Q将禁用元字符,直到找到\\E或模式结束。

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ($source =~ m/\Q$pattern/) {
  print "Match found!"
}

http://www.anaesthetist.com/mnm/perl/Findex.htm http://www.anaesthetist.com/mnm/perl/Findex.htm

Use quotemeta() : 使用quotemeta()

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = quotemeta("Hello_[version]");
if ($source =~ m/$pattern/) {
  print "Match found!"
}

You are using the Wrong Tool for the job. 您正在使用错误的工具来完成工作。

You do not have a pattern! 你没有模式! There are NO regex characters in $pattern! $ pattern中没有正则表达式字符!

You have a literal string. 你有一个文字字符串。

index() is for working with literal strings... index()用于处理文字字符串...

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ( index($source, $pattern) != -1 ) {
    print "Match found!";
}

You can escape set of special characters in an expression by using the following command. 您可以使用以下命令转义表达式中的一组特殊字符。

expression1 = 'text with special characters like $ % ( )'; expression1 ='带有特殊字符的文本,如$%()';

expression1 =~s/[\\?*+\\^\\$[]\\(){}\\|-]/"\\$&"/eg ; expression1 = ~s / [\\?* + \\ ^ \\ $ [] \\(){} \\ | - ] /“\\ $&”/ eg;

This will escape all the special characters 这将逃脱所有特殊字符

print "expression1'; # text with special characters like \\$ \\% ( ) print“expression1”;#text包含特殊字符,如\\ $ \\%()

Quoting a $pattern defeats the purpose of Regular Expressions unless its being used as a known literal and being dumped into a real regex. 引用$pattern破坏正则表达式的目的,除非它被用作已知字面并被转换为真正的正则表达式。

edit 编辑
Otherwise, just use index() to find the position of the substring. 否则,只需使用index()来查找子字符串的位置。 With that information just use substr() to extract surrounding data if necessary. 有了这些信息,只需使用substr()在必要时提取周围的数据。

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

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