How to replace a followed by a numeric character with the numeric character that follows it with a Perl regex?

Does anyone know how to use the regex to replace ", / d" with only the number "/ d"? I tried using / d and it will not work ... it will replace the value followed by any number with the alphabet character "d".

Thanks in advance.

0
source share
2 answers

Just remove any comma that precedes the number using the prediction :

s/,(?=\d)// 

If you tried s/,\d/\d/ , Perl will consider the replacement as a double-quoted string. Since there is no escape code \d , the backslash is simply ignored and d used.

If you want to substitute a match with part of the match, you need to use captures (see ysth answer).

My above substitution does not include a digit in the matched string, so just substituting a comma for an empty string (i.e. deleting it), but still claims that the digit is followed by a comma.

+5
source

You commit what you want to save and use as a replacement:

 s/,(\d)/$1/; 
+5
source

All Articles