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.
>Solution :
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