Anyone knows how to use regular expression to replace “,/d” with just the number “/d”? I tried using /d and it wouldn’t work…it would replace the , followed by whatever number with the alphabet character “d”.
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just remove any comma that precedes a number, using a look-ahead:
If you tried
s/,\d/\d/, Perl would view the replacement as a double quoted string. As there is no escape code\d, the backslash is simply ignored, anddused.If you want to substitute the match with a part of the match, you have to use captures (see ysth’s answer).
My above substitution doesn’t include the digit in the matched string, therefore just substituting the comma with the empty string (i.e. deleting it), but still asserts that the comma is followed by a digit.