-3

I have written this line to replace + and = with space. But it returns an error stating

Quantifier follows nothing in regex

$element= ~ s/+/ /g;
$element= ~ s/=/ /g;

Is this correct? How do I combine both conditions in one?

0

1 Answer 1

6

+ has a special meaning in regexes: it's a quantifier meaning "1 or more". To use + literally, backslash it.

$element =~ s/\+/ /g;

But, if you want to replace both + and =, you can add them to a character class, where + loses its special meaning:

$element =~ s/[+=]/ /g;

Note that tr is faster.

$element =~ tr/+=/ /;
3
  • 1
    Thank you so much.. It works :) Can you also let me know how do I write an array to any file. Will Print FILEPOINTER(@ArrayName) will work?
    – Kajal_T
    Jun 14, 2016 at 9:47
  • 2
    To ask a new question, ask a new question. "Thank you" - upvote/accept the answer.
    – choroba
    Jun 14, 2016 at 9:50
  • Or, using tr///, you can write $element =~ tr/+=/ /
    – Borodin
    Jun 14, 2016 at 11:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.