简体   繁体   中英

Unmatched ( in regex; marked by <— HERE when replace a word wrapped in half ()

This one file contains one line of text:

<p>detached, (disconnected, unfastened</p>   

I need the three words separated by , to be wrapped in <a></a> tags, like this:

<p><a href="entry://detached">detached</a>, <a href="entry://(disconnected">(disconnected</a>, <a href="entry://unfastened">unfastened</a></p>

When I run

perl -pE'/<p>\K.*?(?=<\/p>)/; @words=split(/, /, $&); $new_string=join ", ", map {qq|<a href="entry://$_">$_</a>|} @words; $_=~s/$&/$new_string/g;' a.txt

It gives the following error:

Unmatched ( in regex; marked by <-- HERE …

This one does work though:

perl -pE's{<p>\K.*?(?=<\/p>)}{join ", ", map {qq|<a href="entry://$_">$_</a>|} split(/, /, $&)}eg' a.txt

I don't know why, because basically behind them is the same idea. How does the first one not work as expected?

I'm on macOS Catalina with Perl v5.28.1 if this matters.

I agree with Håkon; the final substitution is interpreting the parenthesis that's in this input stream, and not finding a match to it. Can you not just print/emit your $new_string?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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