Regex.Matches() problem
-
Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin
life is study!!!
-
Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin
life is study!!!
You probably want this: Regex rx = new Regex ("(>=|<=|=)\d+"); x.Matches(str); This matches all numbers with a ">", ">=", "<=" before it, no matter where in the text it is. It also matches in-text, it would for example also match "thisisatext>=500", which you could exclude by adding "\b"'s for word-end-checking, like this: Regex rx = new Regex ("\b(>=|<=|=)\d+\b");
-
Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin
life is study!!!
Two things: 1. You are REQUIRING your match to occur for the entire string since you are using '^' and '$' at the beginning and end, respectively, and 2. You may need to escape the '>' and '<' characters. Implementing both of the above, your regex becomes...
Regex rx = new Regex(@"(\>=?|\<=?|=)\s*\d+\s*");
Let me know if this works as desired,
Sounds like somebody's got a case of the Mondays -Jeff
-
You probably want this: Regex rx = new Regex ("(>=|<=|=)\d+"); x.Matches(str); This matches all numbers with a ">", ">=", "<=" before it, no matter where in the text it is. It also matches in-text, it would for example also match "thisisatext>=500", which you could exclude by adding "\b"'s for word-end-checking, like this: Regex rx = new Regex ("\b(>=|<=|=)\d+\b");
-
Two things: 1. You are REQUIRING your match to occur for the entire string since you are using '^' and '$' at the beginning and end, respectively, and 2. You may need to escape the '>' and '<' characters. Implementing both of the above, your regex becomes...
Regex rx = new Regex(@"(\>=?|\<=?|=)\s*\d+\s*");
Let me know if this works as desired,
Sounds like somebody's got a case of the Mondays -Jeff