0

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

stema
  • 90,351
  • 20
  • 107
  • 135
Ally Tsai
  • 103
  • 1
  • 1
  • 5
  • 4
    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 02:58
  • What tool are you using? – genio Jun 25 '12 at 02: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 03:05
  • 3
    Welcome to Stack Overflow. Please improve your question by posting some [properly formatted](http://stackoverflow.com/editing-help) code, all **relevant** error messages exactly as they appear, and whatever samples you're testing against. – Todd A. Jacobs Jun 25 '12 at 03:35
  • This may also be related to your question http://stackoverflow.com/questions/7690305/regex-to-test-if-a-string-ends-with-a-number . – swapy Jun 25 '12 at 04:12

2 Answers2

0

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

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

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

stema
  • 90,351
  • 20
  • 107
  • 135
  • 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 09:18