These are the requirements but I guess it's too complicated for my regular expression skills...
. between 6 and 10 alphanum characters
. allowed A-Z,a-z,0-9,#,$,_
. Must begin with a letter
. Must contain at least one number
. cannot contain two consecutive identical characters
. cannot contain two consecutive identical numbers
I know the basic of regular expression such as
[A-Za-Z] = characters only etc... but when it comes to consecutive character and stuff...
Try this
((?=.*\d)(?=.*[a-z])(?=.*[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
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{6,20} # length at least 6 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.
string pattern1 = #"^[a-zA-Z]([a-zA-Z])*"; //start and any number of characters
string pattern2 = #"[0-9]+"; //one number or more numbers
string pattern3 = #"[##$%]*"; // special symbol allowed
string pattern4 = #"(.)\1";//consecutive characters
string pattern5 = #"^(.){6,10}$"; //min max
should you want to validate the password you can use groups to do soo ;
(?<a>[a-zA-Z])?(?<b>[0-9])?(?<c>[#%$#/\\\(\)])?
Will give you a match in any of the 3 groups (a,b and c)
uper and lower characters will be in group a
numeric characters will be in group b
and special characters will be in group c
you can use the regex.match.groups("a").count to see if any characters from group a could be found
if you find characters in all 3 groups, the password is strong.
Related
I want to check string which look like following
1st radius = 120
and
2nd radius = 'value'
Here is my code
v1 = new Regex(#"^[A-Za-z]+\s[=]\s[A-Za-z]+$");
if (v1.IsMatch(singleLine))`
{
...
...
}
Using #"^[A-Za-z]+\s[=]\s[A-Za-z]+$" this expression 2nd string is matched but not first and when used this #"^[A-Za-z]+\s[=]\s\d{0,3}$" then only matched first one.
And i also want to check for radius = 'val01'
Basing on your effort, it looks as if you were trying to come up with
^[A-Za-z]+\s=\s(?:'[A-Za-z0-9]+'|\d{1,3})$
See the regex demo. Details:
^ - start of string
[A-Za-z]+ - one or more ASCII letters
\s=\s - a = char enclosed with single whitespace chars
(?:'[A-Za-z0-9]+'|\d{1,3}) - a non-capturing group matching either
'[A-Za-z0-9]+' - ', then one or more ASCII letters or digits and then a '
| - or
\d{1,3} - one, two or three digits
$ - end of string (actually, \z is safer when it comes to validating as there can be no final trailing newline after \z, and there can be such a newline after $, but it also depends on how you obtain the input).
If the pattern you tried ^[A-Za-z]+\s[=]\s[A-Za-z]+$ matches the second string radius = 'value', that means that 'value' consists of only chars A-Za-z.
In that case, you could either add matching digits to the second character class:
^[A-Za-z]+\s=\s[A-Za-z0-9]+$
If you either want to match 1-3 digits or at least a single char A-Za-z followed by optional digits:
^[A-Za-z]+\s=\s(?:[0-9]{1,3}|[A-Za-z]+[0-9]*)$
The pattern matches:
^ Start of string
[A-Za-z]+\s=\s Match the first part with chars A-Za-z and the = sign (Note that = does not have to be between square brackets)
(?: Non capture group
[0-9]{1,3} Match 1-3 digits (You can use \d{0,3} but that will also match an emtpy string due to the 0)
| Or
[A-Za-z]+[0-9]* Match 1+ chars A-Za-z followed by optional digits
) Close non capture group
$ End of string
Regex demo
I have created the following search patterns:
1) Search numbers within given range and excludes specific numbers (excludes 1,2,8)
string numberPattern = #"^([3-7|9 ]*)$";
2) Search letters within given range and excludes specific characters (excludes B,V)
string characterPattern = #"^(?:(?![BV])[A-Z ])+$";
And there can be three kind of inputs:
Input can be just characters: ANRPIGHSAGASGG
Input can be just numbers: 34567934567967
Input can be letters and numbers: 9ANRPIG34HS56A
Question:
Is there a way to tell regex, if using number pattern then it ignores characters and same for character pattern, that it would ignore numbers? The data just can be mixed, in mixed order, I just don't see other way than grouping numbers and characters in different lists and then use related pattern. Is there a way to accomplish that using only regex?
I suggest using
^[3-79A-Z -[BV]]*$
See the regex demo.
Details:
^ - a start of a string anchor
[3-79A-Z -[BV]]* - zero or more (*) characters:
3-79A-Z - digits from 3 to 7, 9, uppercase ASCII letters and a space except B and V ASCII letters (the -[BV] is a character class subtraction construct)
$ - end of string anchor.
Put it into a more readable state so you can maintain it.
^(?:[0-9A-Z](?<![128BV]))+$
Explained
^ # Beginning of string
(?: # Cluster group
[0-9A-Z] # Initially allow 0-9 or A-Z
(?<! [128BV] ) # Qualify, not 1,2,8,B,V
)+ # End cluster, must be at least 1 character
$ # End of string
I'm trying to get one regex that'll do the following:
Min length's 6 and max. length 8
No white-space characters
There is at least one alphabetic character and numeric character
Not contains Turkish character (ı,ö,ğ,ç,ş,ü)
Exp (valid pass) ; tester1, TESTER1, 12345a,
invalid pass ; tester*,tester%
I've this regex : ^.*(?=^.{6,8}$)(?=.*[a-z])((?=.*\d)|(?=.*[A-Z])|(?=.*[\W])).*$
Can somebody show and teach me how to do this?
Here we go:
^(?=.*\d)(?=.*[a-zA-Z])[^öÖşŞıİğĞ]{6,8}$
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters or one uppercase characters
.{6,8} # length at least 6 characters and maximum of 8
| = OR example : (?=.*\d)|(?=.*[A-Z]) = must contains one digit from 0-9 OR must contains one uppercase characters
Thx #Özkan
[^öÖşŞıİğĞ] doesnot allow the following characters öÖşŞıİğĞ
You can test it here
I need to match a string under the following conditions using Regex in C#:
Entire string can only be alphanumeric (including spaces).
Must be a maximum of 15 characters or less (including spaces).
First & last characters can only be a letter.
A single space can appear multiple times in anywhere but the first and last characters of the string. (Multiple spaces together should not be allowed).
Capitalization should be ignored.
Should match the WHOLE word(s).
If any one of these preconditions are broken, a match should not follow.
Here is what i currently have:
^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$
And here are some test strings that should match:
Stack OverFlow
Iamthe greatest
A
superman23s
One Two Three
And some that shouldn't match (note the spaces):
Stack [double_space] Overflow Rocks
23Hello
ThisIsOver15CharactersLong
Hello23
[space_here]hey
etc.
Any suggestions would be much appreciated.
You should use lookaheads
|->matches if all the lookaheads are true
--
^(?=[a-zA-Z]([a-zA-Z\d\s]+[a-zA-Z])?$)(?=.{1,15}$)(?!.*\s{2,}).*$
-------------------------------------- ---------- ----------
| | |->checks if there are no two or more space occuring
| |->checks if the string is between 1 to 15 chars
|->checks if the string starts with alphabet followed by 1 to many requireds chars and that ends with a char that is not space
you can try it here
Try this regex: -
"^([a-zA-Z]([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])$"
Explanation : -
[a-zA-Z] // Match first character letter
( // Capture group
[ ](?=[a-zA-Z0-9]) // Match either a `space followed by non-whitespace` (Avoid double space, but accept single whitespace)
| // or
[a-zA-Z0-9] // just `non-whitespace` characters
){0,13} // from `0 to 13` character in length
[a-zA-Z] // Match last character letter
Update : -
To handle single characters, you can make the pattern after 1st character optional as nicely pointed by #Rawling in comments: -
"^([a-zA-Z](([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])?)$"
^^^ ^^^
use a capture group make it optional
And my version, again using look-aheads:
^(?=.{1,15}$)(?=^[A-Z].*)(?=.*[A-Z]$)(?![ ]{2})[A-Z0-9 ]+$
explained:
^ start of string
(?=.{1,15}$) positive look-ahead: must be between 1 and 15 chars
(?=^[A-Z].*) positive look-ahead: initial char must be alpha
(?=.*[A-Z]$) positive look-ahead: last char must be alpha
(?![ ]{2}) negative look-ahead: string mustn't contain 2 or more consecutive spaces
[A-Z0-9 ]+ if all the look-aheads agree, select only alpha-numeric chars + space
$ end of string
This will also need the IgnoreCase option setting
I have the following code to validate usernames for an application:
Regex usernameRegex = new Regex("[A-Za-z0-9_]");
if (usernameRegex.IsMatch(MyTextBox.Text)) {
// Create account, etc.
}
How would I modify my regular expression to check if the username has a certain number of characters?
This expression validates only all text which contains any combination of A to Z, a to z and number 0 to 9. You can define the length of the string using the regex:
Regex reg= new Regex(#"^[A-Z]{3,}[a-z]{2,}\d*$")
{3,} and {2,} mean here that the string must have at least 3 capital characters, at least 2 small characters, and any amount of digit characters.
For example :
Valid : AAAbb, AAAbb2, AAAAAAbbbbb, AAAAAbbbbbb4343434
Invalid: AAb, Abb, AbAbabA, 1AAAbb,
To set a minimum (or maximum) range in a regular expression you can use the {from,to} syntax.
The following will only match a string with a minimum of 5 alpha numeric and underscore characters:
[A-Za-z0-9_]{5,}
And the following will match a minimum of 5 and maximum of 10:
[A-Za-z0-9_]{5,10}
[A-Za-z0-9_]
[] "brackets": are a group of characters you want to match.
A-Z: means it will match any alphabet capitalized within this range A-Z.
a-z: means it will match any small alphabet within this range a-z.
0-9: means it will match any digit in this range 0-9.
_: means it will match the "_" character.
now this regex will usually match the following: any character from a to z (small, capital), any number (from 0-9) and the underscore "_".
i.e. "a.,.B.,10.._" this will match "a, B, 10, _". but of course you need to add the singleline regex option.