I have a text, how can I replace all numbers in it with themselves just one higher?
I've tried things like the following:
$buffer_content=~s/(\d)/($1++)/g;
The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/; The PATTERN is the regular expression for the text that we are looking for.
!~ is the negation of the binding operator =~ , like != is the negation of the operator == . The expression $foo !~ /bar/ is equivalent, but more concise, and sometimes more expressive, than the expression !($foo =~ /bar/)
(\S+) | will match and capture any number (one or more) of non-space characters, followed by a space character (assuming the regular expression isn't modified with a /x flag). In both cases, these constructs appear to be one component of an alternation.
Use s///e
- evaluation modifier and you can put arbitrary perl codes in second part.
$x = "hello 3";
$x =~ s/([0-9]+)/$1 + 1/eg;
print $x;
// hello 4
ref: http://perldoc.perl.org/perlretut.html#Search-and-replace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With