c# Regex to match end of string?
Solution 1
^[^\(\)]*\(\d+\)$
will do the job...
\d = [0-9]
Solution 2
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
Sandeep
.Net programmer with 7 years experience of working on different Microsoft technologies. My core experience is in asp.net, mvc and sql server. I like put my development skills to good use for something fun and learning new technologies.
Updated on June 04, 2022Comments
-
Sandeep almost 2 years
I need help to generate regex so that it will match any string which has following detail correct:
- String should end with bracket and only numbers inside it.
- End bracket should appear only once at the end of line and not any where else.
- Any characters are allowed before bracket start
- No characters are allowed after bracket end
- String should contain only 1 set of brackets with numbers, i.e. no double brackets like (( or ))
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)
-
Sebastian Schumann almost 9 yearsIt matches
XYZ (1)(2)
,asds-xyz ((1)
,xyz ((2)
andxyz(1)BXZ(2)
but shouldn't. -
Sebastian Schumann almost 9 yearsIt matches
xyz(1)BXZ(2)
but shouldn't. -
n00b almost 9 years@Verarind no, with ^ in the beginning it will not
-
Sandeep almost 9 yearsThanks. Its working fine with test strings. negate group is what i was missing
-
Sebastian Schumann almost 9 yearstest it! Your regex says no bracket
[^\(\)]
for any times*
. Any could be zero and that happens in all of the specified cases. But what's about the second fact that a brace shouln'd come anywhere in the string but the end? You need an ancor^
that all from begining should not be a bracked. But this ancor is missing.