i have got a code to edit some function. There is a text box in that web application. It using a regular expression validator control to validate the text box. the validation expression is
ValidationExpression="[\w]{3,15}"
it accept all letters,numbers and underscores. but it do not accept special characters like \,/ * . i want to change the above regular expression to accept / .
i hope someone can explain what the above regular expression means and how to change that expression to accept / without affecting current regular expression
i am using asp.net and c#
string ValidationExpression= "[\w/]{3,15}"
[...] match a single character presents in the list between brackets
[...]{3,15} match between 3 and 15 characters presents between brackets
\w match a word character (letter, digit, underscore...)
/ match the character /
So [\w/]{3,15} match a word character or '/' between 3 and 15 times.
You current regular expression can be deconstructed as follows :
[] brackets represents regular expression group. Regex engine will try to match all the characters or group of characters given inside [] with the input string.
\w - Allow all the alpha numberic characters which includes upper case and lower case alphabets and 0 to 9 numbers and and underscore (This does not include other special characters like / or # ,etc ).
{3,15} means minimum 3 and maximum 15 alphanumeric characters must be provided in order to successfully match the string.
To add other charters, you need to add them explicitly. If you want to add / your regex should be like [\w/]{3,15}.
You can learn everything about regex here.
Related
I need to use an asp:RegularExpressionValidator control in my asp.net site and I want to restrict the characters to digits and semi-colon only.
So it needs to be able to take any amount of digits and ;
Valid values:
123
123;456
123;456;789;....
What is the regex for that?
Try this simple regex: [0-9;]+ or [\d;]+
\d match a digit [0-9]
; the literal character ;
+ means that it match between one character and unlimited times, as many times as possible,
If you want to guarantee that at least numbers are present in your expression you could do this too:
#npinti has a valid point better will be: ^[\d;]+$
where ^ indicates the begging of your expression and $ the end of it.
Online Demo
What is the regex that matches these examples(6 characters, first is a letter, others are numbers):
u78945 - valid
s56123 - valid
456a12 - invalid
78561d - invalid
1234567 - invalid
i don't know if regular expressions are the same for every programming language. I need it for Regular Expression Validator control using VB ASP.NET.
Use this pattern:
^[a-z][0-9]{5}$
This will match any Latin letter (lower-case unless using case-insensitive matching) followed by 5 decimal digits.
Note: You could use \d instead of [0-9], but read this for an explanation about why they are different.
[a-zA-Z]\d{5}
If you are searching explicitly from the beginning of the line use ^
^[a-zA-Z]\d{5}
and append $ for the end of the line.
^[a(?i)-z(?i)]\d{5}$
The (?i) code enables the expression to accept any letter without case-sensitivity. The \d{5} looks for a sequence of numbers whose length is exactly 5.
I need a regular expression to validate whether text entered in an asp.net textbox has the following format
A-za-z123456789 /s = /s A-za-z123456789
Regular expression explained:
one or more alphanumeric characters
followed by any number of spaces
an equal sign
followed by any number of spaces
one or more alphanumeric characters
[a-zA-Z0-9]*\s*\=\s*[a-zA-Z0-9]*
Replace * with + if you want one or more rather than "any" (which includes zero)
Considering your answer to the comment about requiring one or more alphanumeric characters each side:
[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+
This version will only match if there is at least one alphanumeric character each side of the "=".
If zero valid
"^[a-zA-Z\\d]+\\s*=\\s*[a-zA-Z\\d]+$"
If zero not valid
"^[a-zA-Z1-9]+\\s*=\\s*[a-zA-Z1-9]+$"
The validator should allow only alphabetic characters (a-z and A-Z), dots(.),comma(,), slash(/) and hyphen (-). Please help to to find out one. Or tell me how to create one customized to my specifications.
I have tried [a-zA-Z,-/.] and it works but my requirements only allow for a maximum of 1 of each of the non-letter characters I specified (.,/-).
Try: ^[A-Za-z]*[-a-zA-Z,/.]{1}[A-Za-z]*$
Explanation
^ Anchor to start of string
[A-Za-z]* may be surrounded by multiple letters
[-a-zA-Z,/.]{1} Only one of the enclosed characters
[A-Za-z]* may be surrounded by multiple letters
$ Anchor to end of string
I have a problem with Regular Expression Validator. It takes 0-9, A-Z and prevents ' and " But it not takes lowercase alphabets. Here is my expression ^[a-z|A-Z|0-9|]+[^\"\']*$
You should use:
^[a-zA-Z0-9]+$
The | , "OR", should be used in groups ([a-z]|[A-Z|..). Also, by adding [^"']*, you allow users to enter phrases like a ##%$^&&&&*&^&*#$##$ (starting with alphanumeric character, followed by any non-quote char).
My suggested RegEx means:
^ <start>
[a-zA-Z0-9] Any alplhanumeric character, case-insensitive
+ at least once
$ <end>
If all you need is that the text contain exclusively letters and digits, you could use:
/^[a-zA-Z0-9]+$/
Notice that there is no | in the character set, all the things you put inside the [] are implicitly "or"ed together. Adding a | would allow for a literal | in the text.
Change the + to * if an empty string is valid. You don't need to exclude quotes specifically, since you're not allowing them in at all.
Shorter version of similar regexp:
^[\w]+$
Note: it will also match non-Latin characters and "_", which may be either good or bad depending on your requirements.