Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of strings and I want to replace it with a new word, s +o I do this: $line='i got a dog and a bear.'; @a=('dog','bear','camel'); $a=join('|',@a); $line=~s/\b($a)\b/beast/g; say $line; Simply works, but the problem is that @a is not hard-coded, but instea +d inputted by the user. The elements in @a will contain special char +acters like | or . etc. How do I make this regex work with the antic +ipation of those characters?

Replies are listed 'Best First'.
Re: regex - replacing an array of string
by Anonymous Monk on Dec 24, 2010 at 03:37 UTC
      If I understand your preperly...
      I have not any information about your possible values of entered strings... If you know about list of possible characters in text, then you can change it after entering string. You can use global replacing like this:
      '|' -> '<vertical-line>'
      '.' -> '<dot>'
      This replaces is true only if user can't entering '<vertical-line>' or '<dot>'... You can use more strange replaces like '|'->'asdqwezxc'
      or any other simplier string-code.
      But you must before this string output use reverse decoding of this values...
      May be in some situations this method will be usefull...