Solved

Regex match for negative and positive numbers

Posted on 2006-10-30
5
2,168 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now