Regex Perl中的正则表达式替换包含双等号的内容

Regex Perl中的正则表达式替换包含双等号的内容,regex,perl,Regex,Perl,我需要Perl中的正则表达式来实现这一点: (== doc_url html/arbitrary_file_name.html ==) 为此: (/doc_assets/legacy/html/arbitrary_file_name.html) 我试过各种各样的东西。我当前的尝试如下所示: $content =~ s!\=\= doc_url ([\w\W]+?)\=\=!/doc_assets/legacy/$1!gis; (在这个特殊的尝试中,我只保留了括号,因为从输入到输出都不会改变

我需要Perl中的正则表达式来实现这一点:

(== doc_url html/arbitrary_file_name.html ==)
为此:

(/doc_assets/legacy/html/arbitrary_file_name.html)
我试过各种各样的东西。我当前的尝试如下所示:

$content =~ s!\=\= doc_url ([\w\W]+?)\=\=!/doc_assets/legacy/$1!gis;
(在这个特殊的尝试中,我只保留了括号,因为从输入到输出都不会改变。)


不管怎样,没有什么对我有用。我猜是
==
把东西扔了。任何帮助都将不胜感激。

我想您需要以下帮助:

s!.*?doc_url (.*?/.*?) .*!(/doc_assets/legacy/$1)!sg

即:

#!/usr/bin/perl
$subject = "(== doc_url html/arbitrary_file_name.html ==)";
$subject =~ s!.*?doc_url (.*?/.*?) .*!(/doc_assets/legacy/$1)!sg;
print $subject;
#(/doc_assets/legacy/html/arbitrary_file_name.html)
.*?doc_url (.*?/.*?) .*

Options: Case sensitive; Exact spacing; Dot matches line breaks; ^$ don’t match at line breaks; Numbered capture

Match any single character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “doc_url ” literally (case sensitive) «doc_url »
Match the regex below and capture its match into backreference number 1 «(.*?/.*?)»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match the character “/” literally «/»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “ ” literally « »
Match any single character «.*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

(/doc_assets/legacy/$1)

Insert the character string “(/doc_assets/legacy/” literally «(/doc_assets/legacy/»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character “)” literally «)»


正则表达式解释:

#!/usr/bin/perl
$subject = "(== doc_url html/arbitrary_file_name.html ==)";
$subject =~ s!.*?doc_url (.*?/.*?) .*!(/doc_assets/legacy/$1)!sg;
print $subject;
#(/doc_assets/legacy/html/arbitrary_file_name.html)
.*?doc_url (.*?/.*?) .*

Options: Case sensitive; Exact spacing; Dot matches line breaks; ^$ don’t match at line breaks; Numbered capture

Match any single character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “doc_url ” literally (case sensitive) «doc_url »
Match the regex below and capture its match into backreference number 1 «(.*?/.*?)»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match the character “/” literally «/»
   Match any single character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “ ” literally « »
Match any single character «.*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

(/doc_assets/legacy/$1)

Insert the character string “(/doc_assets/legacy/” literally «(/doc_assets/legacy/»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character “)” literally «)»

正则表达式中的
=
没有什么特别之处,它不应该丢弃它。您的代码对我来说很有用,只是结果中
.html
之间有一个空格。这是因为
([\w\w]+?)
匹配文件名后的空格。正则表达式可以减少为