$keyword =~ tr/+/ /;
That's quicker and a little simpler, since the regex engine isn't invoked.
| [reply] [Watch: Dir/Any] [d/l] |
Don't use a character class on the replacement side of a search/replace. If you want a space, use ' '. Even if \s worked there, it'd be ambiguous since it could be a space, a tab, or something else. See perlre.
| [reply] [Watch: Dir/Any] |
When using
$keyword =~ s/\+/' '/g;
After replacement of
Auto+Recycling+%26+Dismantling
I got
Auto' 'Recycling' '%26' 'Dismantling
Instead of
Auto Recycling %26 Dismantling
| [reply] [Watch: Dir/Any] |
Don't include the quotes. The quotes are there in my node to quote the string. They are not part of the string they are quoting.
| [reply] [Watch: Dir/Any] |
You left out a few important details (and code tags). What was the original string, and the actual regex that was supposed to apply for extracting $keyword?
An empty result means that your initial regex match is not working as expected, so if you showed us the original string and the initial regex, we'd be able to tell you why it failed.
If your initial regex only had one set of parens, then there is no $2 that results from the match -- you need to use $1 instead. Apart from that, if a string contains "+", then your statement s/\+/\s/g will replace each "+" with a literal "s" -- things like "\s, \d, \b" don't work in the right-hand (replacement) side like they do in the left-hand (match) side (but things like "\n, \t, \r" do).
You want to say s/\+/ /g instead.
P.S.: learn about using code tags by looking at Writeup Formatting Tips. You'll see a link to that page every time you preview a new post. Use it. | [reply] [Watch: Dir/Any] [d/l] [select] |