Identifying allowed characters using Regular Expression

H

hungrymind

Hi all,

I am developing some control (textbox based), to validate inputs to
that control I am using regular expression, where pattern is generated
dynamically. I need to identify what all charcters are allowed with
that particular pattern. Is this possible or any way to solve this. Or
else, i want to identify if only one character is allowed then that
character will apear & user need not to enter any value for that.
Please suggest if its possible.

regards,
- hungrymind
http://www.hungrymind.co.in
 
M

MumboJumbo

yes you can do this i believe ...

foreach (Match mymatch in REGEX.matchs(inputstr) {

if (inputstr.Length = mymatch.Length) {
//is ok
} else {
//not ok
}

}
 
H

hungrymind

Thanks James for help,

Here is the idea. There is a property in my control called Expression.
This will have values like
[1]#2[0-4]#[-]#3[0-9]#[-]#4[0-9]

Based on numbers coming after before [ is used to calculate no of text
boxes to be displayed (1 is assumed if such no does not exists) . All
text boxes can contain single character only.
All values contained in [] in Expression property are extracted & used
as regular expression for corresponding text boxes.
Like [1] means only 1 is allow in first text box. 0-4 is allowed in
next 2 text boxes and so on. What all I need is identify allowed
characters for each text box when control loads in the form so that if
a particular text box has only one charcter allowed, that will appear
on load itself & text box becomes uneditable.

This is the idea. The code i have written is not very much descriptive
at this point, I think concept will do for now.

Thanks & regards,
- hungrymind
http://www.hungrymind.co.in
 
H

hungrymind

Please remmeber, i have to identify before any string in input in
textbox, which means I can not use match to evaluate what all text are
allowed like MumboJumbo has suggested
foreach (Match mymatch in REGEX.matchs(inputstr) {
 
J

James Curran

OK, I'm going to assume you've already gotten the code to separate
[1]#2[0-4]#[-]#3[0-9]#[-]#4[0-9]
into [1] [0-4] [0-4] [-] [0-9] [0-9] [0-9] [-] [0-9] [0-9] [0-9]
[0-9]

Now, to determine if it can only be one character, first eliminate all
where there is more than one character bewteen the brackets. Here, that
leaves us with just [1] [-] [-]. Now, we just have to look at that one
character. There are very few one character regular expression special
characters that would be meaningful in this context. Basically, there's "."
(accept any character). If it's anything beside ".", than that's your one
character.

The only think you have to worry about that, is the few characters that
require two characters to represent in a RE: Characters like . $ ^ { [ ( | )
* + ? \ which need to be escaped .
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
H

hungrymind

Thanks James, You got exactly what I wanted. Playing with strings was
in my mind, & seems working fine. But anyhow, I didnt like the
approach. This kind of alternative, I was looking for some solution
which will really describe what all charcters are allowed. This will
help me in giving error message like " only 0,1,2,3,4 are allowed in
this field" and also in future as well. Thanks anyway, help
appreciated. :)

- hungrymind
http://www.hungrymind.co.in
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top