I want to identify two kinds of string: one end with numbers (or not with) by regular expression.
But I can't classify them by \d+$
or ^\D*\d*$
or ^[a-zA-Z]*[0-9]*$
Does anyone can tell me why? and how to solve this problem..?
I want to identify two kinds of string: one end with numbers (or not with) by regular expression.
But I can't classify them by \d+$
or ^\D*\d*$
or ^[a-zA-Z]*[0-9]*$
Does anyone can tell me why? and how to solve this problem..?
\d+$
should indeed work (as should \d$
as minopret noted in his comment) if you use your regex engine's search
method (as opposed to its match
method which in some cases requires the entire string to match).
However, both ^\D*\d*$
and ^[a-zA-Z]*[0-9]*$
are unsuitable for this task because in both of these, the trailing digits are optional (the *
allows zero repetitions, the +
requires at least one).
First your answer is quite incomplete, that makes it quite difficult to answer. Your comment does also not clarify anything.
My guess:
With your two expressions ^\D*\d*$
and ^[a-zA-Z]*[0-9]*$
, you can't verify what you are looking for, since the quantifier *
is also matching 0 occurrences ==> it matches the empty string.
So, ^\D*\d*$
would match any string that has non-digits at the start, or the empty string, followed by digits or the empty string till the end.
Means, this expression would match.
but not
See it here on Regexr