alice7
alice7

Reputation: 3880

Look to get number using regex from string

Looking for a regex to get the number from a string.

My string could be:

abcd1
abcd01
abcd11

I tried this but it is not working: /\d+$/ and some others but they are not seems to be correct.

Is there any easy way to get the number from a string? ANd it will be at the end.

Upvotes: 0

Views: 72

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

I think it is that you want. "Is there anyway to just get 1 instead of 01?" Yes, there is.

[1-9]\d*$

Upvotes: 3

Justin Pihony
Justin Pihony

Reputation: 67135

I believe you want this regex (without the beginning slashes as C# does not need that)

\d+$

To ignore leading 0

[1-9]\d*$

If you want to drop ALL leading 0's, it honestly would be easier to just cast your result to an Int32

Upvotes: 4

Related Questions