Asp.net RegEx Validation - asp.net

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

Related

Extract up to two more digits

This may be a very simple question but I have not much experience with regex expressions. This page is a good source of regex expressions but could not figure out how to include them into my following code:
data %>% filter(grepl("^A01H1", icl))
Question
I would like to extract the values in one column of my data frame starting with this A01H1 up to 2 more digits, for example A01H100, A01H140, A01H110. I could not find a solution despite my few attempts:
Attempts
I looked at this question from which I used ^A01H1[0-9].{2} to select up tot two more digits.
I tried with adding any character ^A01H1[0-9][0-9][x-y] to stop after two digits.
Any help would be much appreciated :)
You can use "^A01H1\\d{1,2}$".
The first part ("^A01H1"), you figured out yourself, so what are we doing in the second part ("\\d{1,2}$")?
\d includes all digits and is equivalent to [0-9], since we are working in R you need to escape \ and thus we use \\d
{1,2} indicates we want to have 1 or 2 matches of \\d
$ specifies the end of the string, so nothing should come afterwards and this prevents to match more than 2 digits
It looks as if you want to match a part of a string that starts with A01H1, then contains 1 or 2 digits and then is not followed with any digit.
You may use
^A01H1\d{1,2}(?!\d)
See the regex demo. If there can be no text after two digits at all, replace (?!\d) with $.
Details
^ - start of strinmg
A01H1 - literal string
\d{1,2} - one to two digits
(?!\d) - no digit allowed immediately to the right
$ - end of string
In R, you could use it like
grepl("^A01H1\\d{1,2}(?!\\d)", icl, perl=TRUE)
Or, with the string end anchor,
grepl("^A01H1\\d{1,2}$", icl)
Note the perl=TRUE is only necessary when using PCRE specific syntax like (?!\d), a negative lookahead.

Regex for exactly 6 char, first must be a letter

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.

Telephone number regex with optional "+"

How would I go about building a regex that allows only digits, with no spaces, and an optional "+" at the beginning?
try this
^\+?\d+$
^ anchors it to the start of the string, $ to the end
\+? is the optional +
\d is a digit and the following + is the quantifier that says at least one (digit).
A useful resource to learn regular expressions is the tutorial of regular-expressions.info
And Regexr is a very useful resource to test regular expressions, see this regex here online
This one should work: ^\+?\d+$
You need to match a +,maybe, followed by digits. The + is a special character, so you need to escape it. To match a telephone number on its own (nothing else in the string) do ^\+?\d+$, to match it in a larger string omit the ^ and $ for just \+?\d+. You can obviously also change \d+ to \d{7} if you know how many digits there should be.
I'm using the following:
(^\+?[0-9]{10,15})$
The + in the beginning is optional as indicated above, with added length restrictions (being minimum 10 digits & maximum 15)

ASP.NET regular expression to restrict consecutive characters

Using ASP.NET syntax for the RegularExpressionValidator control, how do you specify restriction of two consecutive characters, say character 'x'?
You can provide a regex like the following:
(\\w)\\1+
(\\w) will match any word character, and \\1+ will match whatever character was matched with (\\w).
I do not have access to asp.net at the moment, but take this console app as an example:
Console.WriteLine(regex.IsMatch("hello") ? "Not valid" : "Valid"); // Hello contains to consecutive l:s, hence not valid
Console.WriteLine(regex.IsMatch("Bar") ? "Not valid" : "Valid"); // Bar does not contain any consecutive characters, so it's valid
Alexn is right, this is the way you match consecutive characters with a regex, i.e. (a)\1 matches aa.
However, I think this is a case of everything looking like a nail when you're holding a hammer. I would not use regex to validate this input. Rather, I suggest validating this in code (just looping through the string, comparing str[i] and str[i-1], checking for this condition).
This should work:
^((?<char>\w)(?!\k<char>))*$
It matches abc, but not abbc.
The key is to use so called "zero-width negative lookahead assertion" (syntax: (?! subexpression)).
Here we make sure that a group matched with (?<char>\w) is not followed by itself (expressed with (?!\k<char>)).
Note that \w can be replaced with any valid set of characters (\w does not match white-spaces characters).
You can also do it without named group (note that the referenced group has number 2):
^((\w)(?!\2))*$
And its important to start with ^ and end with $ to match the whole text.
If you want to only exclude text with consecutive x characters, you may use this
^((?<char>x)(?!\k<char>)|[^x\W])*$
or without backreferences
^(x(?!x)|[^x\W])*$
All syntax elements for .NET Framework Regular Expressions are explained here.
You can use a regex to validate what's wrong as well as what's right of course. The regex (.)\1 will match any two consecutive characters, so you can just reject any input that gives an IsValid result to that. If this is the only validation you need, I think this way is far easier than trying to come up with a regex to validate correct input instead.

Regular Expression Password Validator

I'm having a hard time trying to create a right regular expression for the RegularExpressionValidator control that allows password to be checked for the following:
- Is greater than seven characters.
- Contains at least one digit.
- Contains at least one special (non-alphanumeric) character.
Cant seem to find any results out there too. Any help would be appreciated! Thanks!
Maybe you will find this article helpful. You may try the following expression
^.*(?=.{8,})(?=.*[\d])(?=.*[\W]).*$
and the breakdown:
(?=.{8,}) - contains at least 8 characters
(?=.*[\d]) - contains at least one digit
(?=.*[\W]) - contains at least one special character
http://msdn.microsoft.com/en-us/library/ms972966.aspx
Search for "Lookaround processing" which is necessary in these examples. You can also test for a range of values by using .{4,8} as in Microsoft's example:
^(?=.*\d).{4,8}$
Try this
((?=.*\d)(?=.*[a-z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{7,20} # length at least 7 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
Use for Strong password with Uppercase, Lowercase, Numbers, Symbols & At least 8 Characters.
//Code for Validation with regular expression in ASP.Net core.
[RegularExpression(#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$")]
Regular expression password validation:
#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$"

Resources