Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regex match for negative and positive numbers

Posted on 2006-10-30
5
Medium Priority
?
2,536 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 1200 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 600 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 200 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

926 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