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
Related
So a sensor I'm interfacing to either outputs 4 multi-digit integers (separated by spaces) or an error string.
Ideally my regex would return a match for either of the above scenarios and reject any
other outputs - e.g. if only 3 numbers are output. I can then check if there are 4 groups (number output) or 1 group (error string output) in the following c#.
The regex I have matches all the time and returns spaces when there are less than 4 numbers so I still need to check everything.
I've tried putting in ?: but the format breaks. Any regex whizzes up to the challenge? Thanks in advance.
([0-9]+\s)([0-9]+\s)([0-9]+\s)([0-9]+)|([a-zA-Z\s_!]+)
So a numeric example would be 11 222 33 4444 or Sensor is in an error state! An incorrect output would be 222 11 3333 as it only has 3 fields
Also - I need to capture the four numbers (but not the spaces) or the error string.
You can capture either the 4 groups with only digits and match the whitespace chars outside of the group.
Or else match 1+ times any of the listed characters in the character class. Note that \s can also match a newline, and as the \s is in the character class the match can also consist of only spaces for example.
To match the whole string, you can add anchors.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[a-zA-Z\s_!]+)$
» Regex demo
Another option to match the error string, is to start matching word characters without digits, optionally repeated by a whitespace char and again word characters without digits.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|[^\W\d]+(?:\s+[^\W\d]+)*!?)$
» Regex demo
If there can be a digit in the error message, but you don't want to match only digits or whitespace chars, you can exclude that using a negative lookahead.
^(?:([0-9]+)\s([0-9]+)\s([0-9]+)\s([0-9]+)|(?![\d\s]*$).+)$
» 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 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
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.
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.