468,104 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,104 developers. It's quick & easy.

Regex to check for numbers with minus and a letter behind them?

Alright so I have a string... that can be anything like this then have a
number like 102.34m, yes there is a m behind it to say "this is money", no I
didn't design the spec thats just how data comes in... well I had a regex
that replaces all those m's in a string where there is a number before it
with no m... so its just the string with 102.34... well this worked fine
until we got negative money! so now when its -102.34m it still has the m...
here is my original regex replace string... any idea on how to change it so
that negative numbers work also? this regex seems to work ok now... (im not
great at regex's... this one took some thinking on my part to figure out so
im not even sure its entirely correct! but it seems to do what i want it
to)... any help would be GREAT! thanks!

/* Look for numbers in a string that have a m behind them and replace the m
with a null string character or space */
(?<=(^|\s)\d*\.\d*)m

/* This needs to be modified to include negative numbers as the thing it is
looking for in addition to posative numbers that are before the m to replace
with a blank string */
Aug 25 '08 #1
3 2285
On Aug 25, 1:56 pm, "Smokey Grindel" <nos...@nospam.comwrote:
/* Look for numbers in a string that have a m behind them and replace the m
with a null string character or space */
(?<=(^|\s)\d*\.\d*)m
if is said that this works, simply by adding a minus sign optional
match before yours digits would work.

(?<=(^|\s)-?\d*\.\d*)m

Thiago
Aug 25 '08 #2
On Mon, 25 Aug 2008 12:56:54 -0400, "Smokey Grindel"
<no****@nospam.comwrote:
>Alright so I have a string... that can be anything like this then have a
number like 102.34m, yes there is a m behind it to say "this is money", no I
didn't design the spec thats just how data comes in... well I had a regex
that replaces all those m's in a string where there is a number before it
with no m... so its just the string with 102.34... well this worked fine
until we got negative money! so now when its -102.34m it still has the m...
here is my original regex replace string... any idea on how to change it so
that negative numbers work also? this regex seems to work ok now... (im not
great at regex's... this one took some thinking on my part to figure out so
im not even sure its entirely correct! but it seems to do what i want it
to)... any help would be GREAT! thanks!

/* Look for numbers in a string that have a m behind them and replace the m
with a null string character or space */
(?<=(^|\s)\d*\.\d*)m

/* This needs to be modified to include negative numbers as the thing it is
looking for in addition to posative numbers that are before the m to replace
with a blank string */
Try this:

^(?:|-)\d+(?:|\.\d+)m$

It matches the following test cases:

102.34m
-102.34m
100m
100.0m
-1m
-10.50m
10m
5m

Note the anchors I have added require a full match. If you want
partial matches (meaning leading and/or trailing additional characters
are okay), remove the anchors like this:

(?:|-)\d+(?:|\.\d+)m

Aug 26 '08 #3
On Tue, 26 Aug 2008 06:51:31 -0400, ja***@onepost.net wrote:

>Try this:

^(?:|-)\d+(?:|\.\d+)m$
Also, it appears you wish to capture the money amounts, so add another
set of parens around the whole thing:

^((?:|-)\d+(?:|\.\d+))m$
Aug 26 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ricky Romaya | last post: by
1 post views Thread by Jon Cooper | last post: by
6 posts views Thread by Vishant | last post: by
8 posts views Thread by vbmark | last post: by
24 posts views Thread by cassetti | last post: by
6 posts views Thread by Gary Bond | last post: by
3 posts views Thread by Praveen | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.