Perl - replace regex with arbitrary work in regex

I have the following line in my Perl script:

s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /g; 

I want to find all the words in a string, and if a word in an array of known words replaces it, save it (ideally, I want to perform an arbitrary coincidence operation, not just a three-dimensional operator).

For this I am trying to use a ternary operator. Does Perl Cure? and: as an alphabetic character and simply concatenates them with variables (if they are defined).

How to get Perl to process ?: internally replace as a ternary operator?

PS: I know that I can just do the coincidence operation on the next line of code, but I want clarity to be one core for it.

+4
source share
1 answer

You need the qualifier 'e' (execute):

 s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /ge; 
+9
source

Source: https://habr.com/ru/post/1332552/


All Articles