0

I have a string that looks like the following:

prop_1=val1 prop_2=val2 prop_3=val3 

I want to replace the entirety of one of the tokens so the string looks like:

prop_1=val1 prop_X=valX prop_3=val3

I have tried

$params =~ s/prop_2=val2/prop_X=valX/ig;

but nothing is changing. What am I doing wrong?

1 Answer 1

2

You are mistaken.

$params = 'prop_1=val1 prop_2=val2 prop_3=val3';
$params =~ s/prop_2=val2/prop_X=valX/ig;
print "$params\n";

gives

prop_1=val1 prop_X=valX prop_3=val3

You have not demonstrated the problem. The only thing I can think of that could cause the above to fail is if pos($params) isn't zero, such as if you perhaps did use /.../g in scalar context earlier, as in

if ($params =~ /.../g)   # Bad

Note that s/(?<!\S)prop_2=val(?!\S)/prop_X=valX/ig would be better as it won't match aprop_2=vals.

Sign up to request clarification or add additional context in comments.

8 Comments

(?<!\S) and (?!\S) would be simpler as \b.
@Jon Purdy, How can something that gives the wrong result be simpler? (e.g. foo.prod_2=val)
Just giving an alternative. There’s no indication in the question of whether it’s right or wrong, as OP’s examples used only word characters. But “word boundary” is certainly simpler than “not preceded/followed by non-whitespace”.
@Jon Purdy, I don't see how you can call (?<!\S)\b and \b(?!\S), simpler than (?<!\S) and (?!\S).
I didn’t. Don’t worry about it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.