Regex: Very basic question
-
Why does this match?
string sSearch = "-- --";
const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
if (Regex.IsMatch(sSearch, sRegEx)) {
// do something funny
}My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Why does this match?
string sSearch = "-- --";
const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
if (Regex.IsMatch(sSearch, sRegEx)) {
// do something funny
}My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
* matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Try this instead:
const string sRegEx = "[a-z]+[A-Z]+[0-9]+";
Thanks for your reply! dratcha wrote: <* matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Yes, but if I use a + instead of a * doesn't that mean i will definetely need 1 char in the range [a-z], plus one in the range [A-Z] and so forth? With the expression I've build in my previous post (using *), I thought to provide a subset of characters which should be allowed as input. But the string '-- --' marched right through claiming it was valid. How come? It wasn't listed in any of the subsets! I just want to understand... :^) /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Thanks for your reply! dratcha wrote: <* matches 0 or more occurences of the previous character or subexpression, while + matches 1 or more. Yes, but if I use a + instead of a * doesn't that mean i will definetely need 1 char in the range [a-z], plus one in the range [A-Z] and so forth? With the expression I've build in my previous post (using *), I thought to provide a subset of characters which should be allowed as input. But the string '-- --' marched right through claiming it was valid. How come? It wasn't listed in any of the subsets! I just want to understand... :^) /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote: But the string '-- --' marched right through claiming it was valid. How come? DId you look at the actual text that was matched? No. Else you would see the match length wasnt 0, in your case it will be. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
Why does this match?
string sSearch = "-- --";
const string sRegEx = "[a-z]*[A-Z]*[0-9]*";
if (Regex.IsMatch(sSearch, sRegEx)) {
// do something funny
}My understanding is, it should allow any number of characters in the range of a-z, A-Z or any number between 0-9. I guess there is something basic I'm missing out here :~ /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams][a-z]*[A-Z]*[0-9]* will match: aA0 aA a A0 a0 aaaaaAAAA0000, etc. I think you are looking for: [a-zA-Z0-9]+ Note the 'match one or more'. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
matthias s. wrote: But the string '-- --' marched right through claiming it was valid. How come? DId you look at the actual text that was matched? No. Else you would see the match length wasnt 0, in your case it will be. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams] -
Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]matthias s. wrote: allowed characters are a-z, A-Z, 0-9 in any quantity In Regex terms thats is an ambigious description. Do you mean? a. any number of a-z followed by, any number of A-Z followed by, any number of 0-9 (
[a-z]*[A-Z]*[0-9]*
). This is also note really correct (it can be, but the Regex wil be complex), but you can check the Length property of the returned Match object if its larger than 0. b. one of more of a-z OR A-Z OR 0-9 ([a-zA-Z0-9]+
or using a character class\w+
that matches any alphabet char or number). Also you want to check the whole input string, so u will need to add SOL/EOL markers. Eg.^\w+$
xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots -
Hi leppie, thanks for your reply. I still don't understand why IsMatch is returning true. For my understanding it should return true if a match has been found. My regular Expression states, that allowed characters are a-z, A-Z, 0-9 in any quantity. It doesn't say that '--' is a valid character. :doh: Could you please provide a short snippet that does the following: Check a given string (say, the SearchString) whether all input characters are falling into these categories a-z, A-Z, 0-9. If the SearchString contains one or more invalid characters, such as : or ; or -, the check should fail and an empty string should be returned. If all characters are valid, the snippet should return the complete SearchString as it was originally passed to the method. I usually don't ask people writing code for me, but I really would like to understand the workings here. Drives me nuts to walk around and make a stupid face... X| Thanks in advance! /matthias
I love deadlines. I like the whooshing sound they make as they fly by.
[Douglas Adams]if u want enter string not contains any special chars. It should contain only alpha numeric then use this code string sSearch = "-- --";const string sRegEx = "[a-zA-Z0-9]"; if (Regex.IsMatch(sSearch, sRegEx)) //if return true { // do something funny } Naveen Sagar