Reputation: 1210
I need help to generate regex so that it will match any string which has following detail correct:
I have tried this .\([0-9]+\)$
but this is not what i required.
For example:
Following string should match:
asds-xyz (1)
asds+-xyz (12)
as@ds-xyz (123)
Following strings should not be matched:
asds-xyz ((1)
asds-xyz ((12sdf))
(123) asds-xyz
xyz ((2)
XYX (1))
XYZ (1)(2)
xyz(1)BXZ
xyz(1)BXZ(2)
Upvotes: 2
Views: 18082
Reputation: 943
Starting from your regex: .\([0-9]+\)$
. match everything but you need quantifier for this. So add * to this
.*\([0-9]+\)$
But the problem is, it will match ( and ) before the last bracket like xyz ((2)
So make a negative set for this, the final result:
^(.*[^\(\)])(\([0-9]+\))$
Upvotes: -1
Reputation: 1852
I try to fix with minimum change to your pattern: you have to use the [^ key to exclude brackets before your only desired bracket. like this
[^\(\)]*\([0-9]+\)$
That would find the patterns you like, and if you like the whole string to be like that, then simply add a ^ in the beginning
Upvotes: 1