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

发布于 2024-12-05 04:31:41 字数 472 浏览 0 评论 0原文

我正在尝试匹配 Perl 中的正则表达式。我的代码如下所示:

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

问题出现在当 Perl 尝试匹配正则表达式时,括号指示字符类(或者我读到的字符类),并且匹配最终失败。我知道我可以使用 \[\] 转义括号,但这需要另一个代码块来遍历字符串并搜索括号。有没有办法自动忽略括号而不单独转义它们?

快速注意:我不能只添加反斜杠,因为这只是一个示例。在我的真实代码中,$source$pattern 都来自 Perl 代码外部(URIEncoded 或来自文件)。

I'm trying to match a regular expression in Perl. My code looks like the following:

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. 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. Is there a way to have the brackets automatically ignored without escaping them individually?

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

澉约 2024-12-12 04:31:41

\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

\Q will disable metacharacters until \E is found or the end of the pattern.

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

长发绾君心 2024-12-12 04:31:41

使用 quotemeta()

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

Use quotemeta():

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = quotemeta("Hello_[version]");
if ($source =~ m/$pattern/) {
  print "Match found!"
}
梨涡 2024-12-12 04:31:41

您使用了错误的工具来完成这项工作。

你没有模式!没有正则表达式
$pattern 中的字符!

你有一个文字字符串。

index() 用于处理文字字符串...

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ( index($source, $pattern) != -1 ) {
    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!

You have a literal string.

index() is for working with literal strings...

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ( index($source, $pattern) != -1 ) {
    print "Match found!";
}
猫性小仙女 2024-12-12 04:31:41

您可以使用以下命令转义表达式中的特殊字符集。

expression1 = 'text with special characters like $ % ( )';

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

#This will escape all the special characters
print "expression1'; # text with special characters like \$ \% \( \)

You can escape set of special characters in an expression by using the following command.

expression1 = 'text with special characters like $ % ( )';

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

#This will escape all the special characters
print "expression1'; # text with special characters like \$ \% \( \)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文