-1

Can anyone explain the meaning of the Regular Expression pattern below in Perl?

s/^(-?\d+)(\d\d\d)/$1,$2/
l'L'l
  • 40,316
  • 6
  • 77
  • 124
  • 1
    It adds a `,` before the last 3 digits. Please make some effort before you ask questions which you can solve by little research on your own. – nu11p01n73R Mar 12 '15 at 06:55

1 Answers1

0

Take the string:

-1234567890

Using the regex pattern:

s/^(-?\d+)(\d\d\d)/$1,$2/
| | |  |    | | |    |
| | |  |     \|/     replace pattern using groups [1],[2]
| | |  |     [2] match a digit [0-9] (3 times)
| | | [1] match a digit between one and unlimited times
| |[1] matches character "-" literally between zero and one time [greedy]
| ^ assert position at start of the string
substitution pattern

Using the original string above would become:

-1234567,890

Example:

https://regex101.com/r/gV9kE9/1

l'L'l
  • 40,316
  • 6
  • 77
  • 124