Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hey guys,

at the moment im going to try to create a RegExp that matchs to integer values in strings.
So i don't have strings like this:
"123" or "123.456"
If that would be the case i could simply use "^[\d]*$" as RegExp.

But like i said, im going to try to get integer values in strings.
Two sample strings could look like this:
A sample integer value is for example 123456 Dollar<br />
Some other decimal value is for example 123.456 Dollar


The goal is, that the RegExp only get a matches the 123456 in the first string and not at 123 or 456 in the second one, because the second string contains a decimal value, not two integer values separated by a dot.

I hope i could make clear what i mean.
If not, feel free to ask.

Thank you so much for your help!

Edit:
I've played with RegExp and found the following two RegExp:
[\d]+(?!\.) all numbers but no number bevore a dot
and
(?<!\.)[\d]+ all numbers but no number after a dot

But i'm confused how to connect them with a logical and operation to get only matches where is no dot bevore a number AND no dot after a number.
Posted
Updated 26-Aug-14 2:33am
v3
Comments
ChauhanAjay 26-Aug-14 5:49am    
try the below link
http://stackoverflow.com/questions/19715303/regex-that-accepts-only-numbers-0-9-and-no-characters
C3D1 26-Aug-14 5:57am    
Hey,
in your link they suggest to use "^\d$".
But i think i've made it clear in my question above that something like ^[\d]*$ doesn't work in my (special) case.

1 solution

Here's a regex that will do what you want:
\b(?<!\.)\d+(?!\.\d)\b

\b is a zero-length anchor, matching a "word" boundary. It ensures that the following digits aren't part of a "word" like "abc123".

(?<!\.) is a negative lookbehind that ensures there is no decimal preceding the digits you want

(?!\.\d) is a negative lookahead that ensures there is no decimal and digit following the digits you want

EDIT: added lookbehind to prevent matching part of a decimal
 
Share this answer
 
v4
Comments
C3D1 26-Aug-14 8:44am    
Hey,

thank you a lot for your solution.
But i have done a quick look in regex101.com and found, that your regex match to the number after the dot:
http://regex101.com/r/cQ5qB6/2

So it's not exactly what i want, but it's really close to it! :)
Brian A Stephens 26-Aug-14 8:51am    
Sorry, I forgot about that stipulation. I've updated it now.
C3D1 26-Aug-14 8:53am    
You are a genious :) Thank you so much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900