Solved

Regex match for negative and positive numbers

Posted on 2006-10-30
5
2,379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

624 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