Solved

Regex match for negative and positive numbers

Posted on 2006-10-30
5
2,235 Views
Last Modified: 2012-06-27
hi:
  I am looking to check if the input string is a negative number or a positive number. How can I do this using Regex i.e I am looking for the pattern string to match all inputs against?

Thanks
0
Comment
Question by:LuckyLucks
5 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 300 total points
ID: 17835283
Hi, try the following regex, it should match both negative and positive numbers:

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

Example:

using System.Text.RegularExpressions;
...
      string data = "-10";
      string regex = @"(-|\+)?\d+(\.\d+)?";

      Match m = Regex.Match(data, regex);

      if (m.Success)
      {
            MessageBox.Show("Matched: " + m.Value);
      }

is that what you were after?
0
 
LVL 6

Assisted Solution

by:der_jth
der_jth earned 150 total points
ID: 17835411
You should add a ^ in front and a $ to the end of Zephyr's regex. Other than that, it'll probably work okay. Without the string terminators (^ and $) it'll match anything that contains the appropriate number ("foo3", "a-5-b" etc. would all match).
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 50 total points
ID: 17838049
Do you want to accept .3 and -.3 (i.e. no leading '0' before the dot?)

In which case, change the middle \d+ to \d*

(Argh.. though that then accepts blank strings, so you might want to check against that, too!)
0
 

Author Comment

by:LuckyLucks
ID: 17851832
Hi Zep,
Could u illuminate me on what the | and the \ geanerally mean and wr.t to the first bracket contents? thx
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17851930
Hi,

The | symbol allows you to match alternative tokens e.g. "cat|dog" will match either cat or dog. In our case we wanted to match either a minus or a plus sign at the beginning. (question mark is explained below)

The backslash \ character is used to escape characters that have special meaning in regular expressions. I escaped the + sign so that the regex engine takes it as a character plus symbol rather than its special meaning "one or more" in regular expressions.

The question mark symbol means "zero or none" of the previous symbol.... "\d" is short hand for any digit in the range 0-9. Finally, the dot was escaped using the backslash as it has a special meaning in regex's but we wanted it to simply mean a dot (decimal point in the input number).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question