0

Hi im trying to create a regex for the following expression

I need matching groups on all the integers

  • Valid
    • integer-integer
    • integer-integer/integer
  • Invalid
    • integer-integer/

So the first part would be like

^(\d+)-(\d+)

which matches 1-50

With these matching groups

  • 1st group: 1
  • 2nd group: 50

what do i need to add to the second part to make

/integer

optional but / should not be in the 3rd matching group and an integer is supplied

4

2 回答 2

1

使用非捕获可选组,并在其中使用整数组。

这应该适合你:

^(\d+)-(\d+)(?:/(\d+))?$
于 2013-05-16T07:02:24.747 回答
1

将斜线放在非捕获组中,将斜线后面的数字放在捕获组中。

^(\d+)-(\d+)(?:/(\d+))?
于 2013-05-16T07:02:36.583 回答