需要帮助立即理解正则表达式

时间:2015-03-12 06:40:42

标签: regex perl

任何人都可以在Regular Expression中解释下面Perl模式的含义吗?

s/^(-?\d+)(\d\d\d)/$1,$2/

1 个答案:

答案 0 :(得分:0)

取字符串:

-1234567890

使用正则表达式模式:

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

使用上面的原始字符串将变为:

-1234567,890

示例:

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