Take the 2-minute tour ×

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..?

share|improve this question
Why, what goes wrong with \d+$ or indeed just \d$ ? Also, what regular expression engine are you using? Different languages and tools have different ones. –  minopret Jun 25 '12 at 2:58
What tool are you using? –  genio Jun 25 '12 at 2:59
They both take StringNumber and StringNumberString as the same one. I'm using Windows XP and just want to run it on Windows Command Processor –  Ally Tsai Jun 25 '12 at 3:05
Welcome to Stack Overflow. Please improve your question by posting some properly formatted code, all relevant error messages exactly as they appear, and whatever samples you're testing against. –  CodeGnome Jun 25 '12 at 3:35
This may also be related to your question stackoverflow.com/questions/7690305/… . –  swapy Jun 25 '12 at 4:12
add comment

2 Answers

up vote 0 down vote accepted

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.

  • the empty string
  • Foobar
  • Foobar 123
  • 123

but not

  • 123Fooo

See it here on Regexr

share|improve this answer
thanks for your explanation, now I finally understand why I can't find the string I want. It should be the problem of *. And I already find another solution for my requirement, thank you so much!! –  Ally Tsai Jun 25 '12 at 9:18
add comment

\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).

share|improve this answer
Thank you, too!! I thought it's because I didn't understand the influence of *. –  Ally Tsai Jun 25 '12 at 9:25
add comment

Your Answer


