Currently, in my program I'm checking my strings to see if they are NOT of an integer value by using the following Regex
method along with an if
statement:
if(Regex.IsMatch(myString, @"^\d+$") == false) {}
However, I need to allow values such as: "45 23 12". These values involve spaces, which I believe are counted as characters by Regex
because whenever I try an example like the previous one, my code returns false.
How do I perform a check for values that aren't integers while allowing the use of spaces?
This question uses Regex.Matches(myString,@"[a-zA-Z]").Count
to check the string for all character occurrences from 'a-z', which doesn't involve " ". However, I notice when I use it in my if
statement that it's of type Int, which can't be compared to false
with ==
.
if(! Regex.IsMatch(myString, @"^\s*\d(\d|\s)*$")) { ... }
I would use the regex
^\d+(\s\d+)*$
Explanation:
^ beginning of line.
\d+ one or more digits.
(\s\d+)* zero, one or more repetitions of space + one or more digits.
$ end of line.
if (!Regex.IsMatch(myString, @"^\d+(\s\d+)*$")) {
...
}
And please write
!<boolean expression>
instead of <boolean expression> = false
.
and
<Boolean expression>
instead of <Boolean expression> = true
.
You don't write 1 * x
but simply x
.
A lot of people think that if-statements require a comparison. They do not. What they require is a Boolean expression, i.e. an expression yielding a value of true
or false
. Regex.IsMatch(...)
returns a bool
value and this is all we need.
Try the following regex (if I understand your question correctly.
^(?:\d|\s)+$
It seems to do what you want and here is the test for it.
Currently, in my program I'm checking my strings to see if they are NOT of an integer value by using the following Regex
method along with an if
statement:
if(Regex.IsMatch(myString, @"^\d+$") == false) {}
However, I need to allow values such as: "45 23 12". These values involve spaces, which I believe are counted as characters by Regex
because whenever I try an example like the previous one, my code returns false.
How do I perform a check for values that aren't integers while allowing the use of spaces?
This question uses Regex.Matches(myString,@"[a-zA-Z]").Count
to check the string for all character occurrences from 'a-z', which doesn't involve " ". However, I notice when I use it in my if
statement that it's of type Int, which can't be compared to false
with ==
.
if(! Regex.IsMatch(myString, @"^\s*\d(\d|\s)*$")) { ... }
I would use the regex
^\d+(\s\d+)*$
Explanation:
^ beginning of line.
\d+ one or more digits.
(\s\d+)* zero, one or more repetitions of space + one or more digits.
$ end of line.
if (!Regex.IsMatch(myString, @"^\d+(\s\d+)*$")) {
...
}
And please write
!<boolean expression>
instead of <boolean expression> = false
.
and
<Boolean expression>
instead of <Boolean expression> = true
.
You don't write 1 * x
but simply x
.
A lot of people think that if-statements require a comparison. They do not. What they require is a Boolean expression, i.e. an expression yielding a value of true
or false
. Regex.IsMatch(...)
returns a bool
value and this is all we need.
Try the following regex (if I understand your question correctly.
^(?:\d|\s)+$
It seems to do what you want and here is the test for it.
0 commentaires:
Enregistrer un commentaire