Regular Expressions
-
I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?
-
I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?
In most regular expression syntaxes, "." is a symbol that means "any character". You probably need to escape the . with a backslash, like "\.".
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins -
I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?
^\d{5}(\.\d{2})?$
You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use.{5}
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I am using the RegEx which needs to check for the value to be five digits before the decimal and 2 after the decimal or just five digits. and I am using System.Text.RegularExpressions.Regex exp = new System.Text.RegularExpressions.Regex("(^\\d{0,5}.{1}\\d{1,2}$)|(^\\d{1,5}$)"); With this it's only checking if the value is five digits before decimal and 2 after but not for just 5 digits. I entered 6 digits without decimal and it did not catch the exception. I don't know what's wrong. Any help ?
I think I would write this as: @"^\d{5}(\.\d{2})?$" Well, actually, I'd write it as: @"^ \d{5} # 5 digits ( \. # decimal point \d{2} # 2 digits )? # match zero or one time " and then use RegexOptions.IgnorePatternWhitespace (it wasn't clear to me if you meant "only 5 digits" or "from 1 to 5 digits" in your description, so you might need to change the first part.
-
^\d{5}(\.\d{2})?$
You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use.{5}
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks for the help It works.:laugh:
-
I think I would write this as: @"^\d{5}(\.\d{2})?$" Well, actually, I'd write it as: @"^ \d{5} # 5 digits ( \. # decimal point \d{2} # 2 digits )? # match zero or one time " and then use RegexOptions.IgnorePatternWhitespace (it wasn't clear to me if you meant "only 5 digits" or "from 1 to 5 digits" in your description, so you might need to change the first part.
I have changed my code and it works. Actually it supposed to be 1 to 5 digits, so I changed the first part to \d{1,5}
-
^\d{5}(\.\d{2})?$
You didn't escape ".". Without escaping, this means that it matches any character or none. For instance, to match any 5 characters, you could use.{5}
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
I'm pretty sure you know this and just had a brain fart while typing, but for anyone else reading this... . actually means any character except possibly "\n" depending on options you set for your regular expressions. In other words, it doesn't match "none". More details here.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins -
I'm pretty sure you know this and just had a brain fart while typing, but for anyone else reading this... . actually means any character except possibly "\n" depending on options you set for your regular expressions. In other words, it doesn't match "none". More details here.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. HubbinsYou're right - brain fart! :) I'm so used to typing
.*
for so many things I practically think of it as a single pattern element! ;P Thanks for the catch!-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----