/    Sign up×
Community /Pin to ProfileBookmark

Help with regex for float-type pattern

Hi. This seems so simple, yet I can’t seem to get it right. I’m trying to write a regular expression for a pattern that looks like an unsigned float (with possible trailing zeros).

For example. I want to match something like “78783.9920”. That’s it!

I’ve tried at least a dozen different expressions, but here’s the most recent one that failed.

var reg = /^[0-9]+.([0-9]+)$/;

I just wanna match some numbers, followed by a period, followed by some more numbers.

I would also be grateful if someone could point me to a good resource to learn regex better. I find that most tutorials online don’t give enough examples (especially for multiple strings tested against the SAME regex), or they just don’t really explain very well WHY or WHY NOT there is a match.

Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rtretheweyMar 16.2012 — Regular expressions aren't my strong suit, but off the top of my head, I would have used:

var reg = /^[0-9]+(.[0-9]+)?$/;

so that the decimal point and any digits that follow it are optional to achieve a match.
Copy linkTweet thisAlerts:
@PacopagauthorMar 16.2012 — Hi. Thanks. That's a good idea. The regex was fine. It was actually the string that had a problem with it (i.e. I thought I was testing one string, but was actually testing another).

Cheers,.
Copy linkTweet thisAlerts:
@007JulienMar 16.2012 — What is the question ? Verify a value is a float number ? Or either find the float numbers in a string ?

For the first question : parseFloat and isNaN are probably the best methods. If the test is to make with a regular expression ^ and $ are useful, with one or more digit (0-9 or d) perhaps (an integer is a float number) followed by a point and one or more digit (see the test infra).

For the second ^ and $ are useless, but a g is necessary to find all the occurrences.

Then this script illustrate the use of the proposed methods
[CODE]var values="3.14159,Anatole,FFCC00,5,0.77,0679ç".split(',');

// Verify values with parseFloat and NaN
for (var i=0;i<values.length;i++) {nok=' ';flt=parseFloat(values[i]);
if (isNaN(flt)) nok=' not ';
alert(values[i]+' => '+flt+' is'+nok+'a Float !');
}
// Verify values with a test
for (var i=0;i<values.length;i++) {nok=' not ';
if (/^d+(.d+)?$/.test(values[i])) nok=' ';
alert(values[i]+' is'+nok+'a Float ! (test)');
}

str="Prices for fruits and vegetables—in fresh and processed forms—vary widely. Fruit prices ranged from 32 cents per pound for fresh watermelon to $4.00 per pound for prunes. Among vegetables, prices ranged from 31 cents per pound for fresh potatoes to $4.57 per pound for frozen asparagus spears.";
str.replace(/d+(.d+)?/g,function(a){alert(" Float number in the string "+a)});[/CODE]
×

Success!

Help @Pacopag spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...