正则表达式问题,避开第一个符号,得到第二个

Regular expression problem, avoid the first sign, and get the second one

我有个小问题。我尝试从字符串中提取一些匹配项,就像这样。但我不知道该怎么做。谢谢

2+22 -> match1: 2; match2: 22

2-22 -> match1: 2; match2: 22

2++22 -> match1: 2; match2: +22

2+-22 -> match1: 2; match2: -22

我不知道您使用的是什么语言,但以下内容似乎适用于那些使用 PHP/PCRE 的测试用例:

(\d+)[+-]([+-]?\d+)

分解:

  • (\d+) 匹配至少一位数字,并在组 1 中捕获它
  • [+-] 匹配加号或减号
  • ([+-]?\d+) 匹配加号或减号或无,后跟至少一位数字。在第 2 组中捕获所有内容。