Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex replace numbers with themselves, just one higher

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;
like image 736
john-jones Avatar asked Oct 26 '12 10:10

john-jones


People also ask

What is \s in Perl regex?

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.

What does !~ Mean in Perl?

!~ 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/)

What does \s+ mean in Perl?

(\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.


1 Answers

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

like image 83
Sjoerd Avatar answered Oct 07 '22 00:10

Sjoerd