0

I have a regular expression in c# that should return IsMatch = true only when the input has the desired pattern but actually is returning true if some of the characters matches...How would be the correct regular expression?

Regex reg = new Regex(@"[0-9 \-+]"); //accept only numbers, spaces, minus character and plus character

string formularight="1123 - 4432+32124";//its correct

bool validformat=reg.IsMatch(formularight)) //returns true, ok

string formulawrong="1123a - 4432+32124"; //it has one letter ismatch should be false...

validformat=reg.IsMatch(formulawrong)) //returns true, not ok

I check later if each number is followed by a minus or plus sign before the next number but if it can be included in the regex validation...

I checked other regex questions and before someone suggest that i use a datatable to compute() the expresion or use some calculator logic please know that in this case the numbers are used like fields names that i will use to get some values from the database not numerical values per se. So i only need the regex validation before parsing the formula. Thanks

Valid examples for regex:
11123
112 - 1121
112-1121
1221111+554-111135678
44332-54-114

Invalid examples (letters present, not a + or - between numbers,...):
112 -
6543e
112 1121
6543e + 4432
-7632
Nick
  • 1,417
  • 1
  • 14
  • 21
VSP
  • 2,367
  • 8
  • 38
  • 59

3 Answers3

2

Your Regular Expression finds several matches, because you didn't force it to match the whole input.

By using the following anchors, it will be forced to check the entire input.

  • ^ - Start
  • $ - End

Regex:

^[0-9 \-+]*$

Regexr: http://regexr.com/3b97l

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
1

You should start and end anchors in your regex:

Regex reg = new Regex(@"^[0-9 +-]+$");

to make sure whole input matches given set of characters.

isMatch Reference

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • With >250k rep, could you afford to search for duplicates? – Rawling Jun 25 '15 at 14:20
  • I'm not a .NET person and hardly answer anything on .NET. I could spot a problem and I verified that by looking up linked MSDN and answered. – anubhava Jun 25 '15 at 14:26
  • There are [non](http://stackoverflow.com/questions/6298566/regex-match-whole-string)-[.NET](http://stackoverflow.com/questions/5997673/regex-to-match-full-string) duplicates too. – Rawling Jun 25 '15 at 14:27
  • I think you're not getting my point. How would one know if `IsMatch` is not same as Java's `String#matches` where anchors are not needed. (At least I didn't know). – anubhava Jun 25 '15 at 14:30
  • Well, if you know enough about .NET to look at MSDN to verify your answer, surely you know enough about .NET to search SO for a .NET duplicate? – Rawling Jun 25 '15 at 14:34
  • Finding MSDN link just takes typing `reg.IsMatch` in my browser, searching SO is not same. – anubhava Jun 25 '15 at 14:39
  • If you're worried about effort, it's even *less* effort to not answer at all and let someone less lazy search for a duplicate. – Rawling Jun 25 '15 at 14:41
  • I won't answer if I find dup easily. Feel free to flag my answer to moderators if you find it so offensive. – anubhava Jun 25 '15 at 14:45
  • 1
    @Rawling, The user didn't knew his problem come from the beginning and the end anchors. He didn't posses the `Regex` as we do. So a search for duplicate may help him, but his question didn't knew that was his problem. If he would ask `Why does my regex doesn't covert the full\exact input` than pointing him to Duplicate will be the right call. – Orel Eraki Jun 25 '15 at 16:16
1

How about:

^\d+(?:\s*[+-]\s*\d+)*$

This works for your valid and invalid examples.

In order to match numbers in brackets:

^\[?\d+\]?(?:\s*[+-]\s*\[?\d+\]?)*$
Toto
  • 89,455
  • 62
  • 89
  • 125
  • If you could please modify the regex so it also accepts numbers between brackets i would gladly accept your answer. Example: 124 + [4211] - 332 or [1121] or [1443] + [1675] - 1111... – VSP Jun 25 '15 at 15:49