c# - Regex expression that match only these cases -


i have regular expression in c# should return ismatch = true when input has desired pattern returning true if of characters matches...how correct regular expression?

regex reg = new regex(@"[0-9 \-+]"); //accept numbers, spaces, minus character , plus character  string formularight="1123 - 4432+32124";//its correct  bool validformat=reg.ismatch(formularight)) //returns true, ok  string formulawrong="1123a - 4432+32124"; //it has 1 letter ismatch should false...  validformat=reg.ismatch(formulawrong)) //returns true, not ok 

i check later if each number followed minus or plus sign before next number if can included in regex validation...

i checked other regex questions , before suggest use datatable compute() expresion or use calculator logic please know in case numbers used fields names use values database not numerical values per se. need regex validation before parsing formula. thanks

valid examples regex: 11123 112 - 1121 112-1121 1221111+554-111135678 44332-54-114  invalid examples (letters present, not + or - between numbers,...): 112 - 6543e 112 1121 6543e + 4432 -7632 

how about:

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

this works valid , invalid examples.

in order match numbers in brackets:

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

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -