-2

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?

Borodin
  • 125,056
  • 9
  • 69
  • 143
Kajal_T
  • 65
  • 8

1 Answers1

7

+ 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/+=/ /;
ikegami
  • 343,984
  • 15
  • 249
  • 495
choroba
  • 216,930
  • 22
  • 195
  • 267