Solved

Regex match for negative and positive numbers

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

18 Experts available now in Live!

Get 1:1 Help Now