A bit of syntax: capture with (?<name>pattern)
and then use that capture with $+{name}
outside of the pattern (delimiters may be varied); see it in perlre. The whole regex
s{ title="(?<t>[^"]+)" (?<text>.*?) name="(?<n>[^"]+)" }
{title="$+{n}"$+{text}}x
The \g{name}
syntax attempted in the question is used inside the pattern itself (if it is needed further in the same pattern in which it first gets captured); but after the matching side, so in the replacement side or after the regex, the matches are retrieved from the %+
variable.
The [^"]
is a negated character-class, matching any character other than "
. The modifier /x
at the end makes it ignore literal spaces inside so we can use them for readability.
A full example, with the above regex, to run on the command line
echo title=\"alpha\" lorem ipsum lorem ipsum name=\"beta\" | perl -wpe
's{title="(?<t>[^"]+)"(?<text>.*?)name="(?<n>[^"]+)"}{title="$+{n}"$+{text}}'
(broken into two lines for readability). It prints
title="beta" lorem ipsum lorem ipsum
Not sure what the first pattern is captured for in the question but perhaps there is more to it than shown so it is captured here as well, into $+{t}
.
Also, the question uses those quotes in a particular way. One can string together '
-delimited strings for one command-line program (perl -wE'say''"hi"'
is valid). The example in the question works since what would be "barewords" (one
etc) happen to be inside regex, where they are OK, as patterns. But I'd suggest not to mess with that (if that was the intent).