I would like to know if we can handle specific input like the following examples using pattern:
[1] or [1,2]
so basically brackets with a number or if there are more numbers then only comma allowed as a separator.
Tried [(\d (\s*,?)) ] as stated below but it doesn't work.
CodePudding user response:
This would do it:
^\[\d (?:,\d )*\]$
^- start line anchor\[- open literal square bracket\d- one or more digits(?:- open non-capturing group,\d- comma followed by one or more digits
)- close non-capturing group*- match pattern inside non-capturing group zero or more times\]- close literal square bracket$- end line anchor
https://regex101.com/r/eVzjRw/1
CodePudding user response:
Use this regex \[(\d (\s*,?)) \]
