I am using Fare library https://github.com/moodmosaic/Fare/ to generate a random string from regular expression. Up until now, it has been working properly.
What I wanted now is
"The Password must have a minimum/maximum of 8 characters, including one special character, atleast 1 digit and atleast capital letter."
Special characters allowed are !#$%^&*()=,.
for that, I have created the expression
^((?=.\d)(?=.[A-Z])(?=.*\W).{8,8})$
But it is not generating valid expression
Please check what's the problem
I am generating the regular expression with:
var secret = new Xeger(ConfigurationManager.AppSettings["expression"]).Generate();
Console.WriteLine(secret);
I have updated the pattern requirement
If Fare doesn't support lookaheads, then you may instead try using a different regex expression devoid of any such lookaheads:
^([0-9])([A-Z])([!##%*()$?+-])([a-zA-Z0-9]{5})$
So this regex expression can be used for validating passwords but we would then be restricting user to input first a digit, second a capital letter, third a special character with rest being a mix of alphanumeric chars.
Related
I've got this Regex as a data annotation on a password field in a C# .NET MVC project.
[RegularExpression("(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*+\\/|!\"£$%^&*()#[\\]#~'?><,.=-_]).{6,}", ErrorMessage = "Password must be between 6 and 20 characters and contain one uppercase letter, one lowercase letter, one digit and one special character.")]
I want to make the following passwords invalid:-
Testing
Testing1
TesTinG123
The following would be valid:-
Testing1*
T&sting123*
%F&y5HD$S
I would like a minimum of 6 characters, with at least one uppercase, one lower case, one digit and one special character. I hope the above regex would do it, but it lets Testing12 and Tester12 through as valid passwords.
What can I change in the above to get what I am expecting? I assumed the above requires one uppercase, one lowercase, one numeric and one special character but obviously I am wrong?
Thanks in advance!
You forgot to escape ] properly (it is advised to even put it at the start of the character class so that in .NET it could be parsed as a literal ] - for the client side validation, it needs to be escaped even there) and remember that a hyphen inside a character class creates a range (e.g. [=-_]), so it must be put at the end/start of the character class or at the end to denote a literal -).
Use
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\]*+\\/|!\"£$%^&*()#[#~'?><,.=_-]).{6,}$"
^^ ^
Or, if you just want to require at least 1 char that is not alphanumeric, use
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{6,}$"
where [\W_] matches any char but a word char (i.e. [^\p{L}\p{N}_] or - on client side - [^a-zA-Z0-9_]) + _.
The ^ and $ anchors are redundant in the RegularExpressionAttribute pattern, but they won't do any harm.
See the regex demo.
I already have an email address regular expression FROM RFC 2822 FORMAT
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
but want to modify it to include the following some new conditions:
at least one full stop
at least one # character
no consecutive full stops
must not start/end with special characters i.e. should only start/end with [0-9a-zA-Z]
should still follow RFC specification for regular expression rules.
Currently the above one allows the email to start with special characters. Also it is allowing two consecutive full stops (except for domain name which is fine, so test#test..com fails and its correct).
Thanks.
^[a-zA-Z0-9]+(?:\.?[\w!#$%&'*+/=?^`{|}~\-]+)*#[a-zA-Z0-9](?:\.?[\w\-]+)+\.[A-Za-z0-9]+$
No .. and at least 1 . and 1 #.
Also starts/ends with letters/numbers.
The ^ (start) and $ (end) were just added to match a whole string, not just a substring. But you could replace those by a word boundary \b.
An alternative where the special characters aren't hardcoded:
^(?!.*[.]{2})[a-zA-Z0-9][^#\s]*?#[a-zA-Z0-9][^#\s]*?\.[A-Za-z0-9]+$
I have a xml file containing certain expressions like this :-
1. AAaaaaa-1111
2. AAaaa-1111-aaa
3. AA11111-11111
4. AA111-111-111111
(AA static text) (aaaa-Any alphabet only) then hyphen (1111 - any digit only)
I was thinking i should write regular expression for these I believe regex should be the right approach.
But this XML file is dynamic. User can remove or add different expressions in the list. So How can i use regular expression here? Is there any dynamic regular expression kind of thing. Show me the light here please.
UPDATE:- I am using these expressions to validate user input. So whatever user is entering in a box, it should be matched with any of these expressions from the list.
For Example:-
If user enters
AAabc-4567-trr
, then it should be validated coz it matches with 2nd expression in the list
Well,
What I assume from your question is that:
A is the letter A
a is any letter
1 is any number
That's the only way I see AAabc-4567-trr matches AAaaa-1111-aaa
Is that correct?
If it is correct, yes, you could use Regular Expressions. What you need to do is translate your patterns to regex patterns. Assuming you have a new pattern:
AAA-aaa-111
to obtain the regex that will recognize that pattern, all you have to do is translate that pattern into regex patterns. For example:
string xmlPattern = "AAA-aaa-111"
string regexPattern = xmlPattern.Replace("a", "[a-zA-Z]").Replace("1", #"\d");
Edit:
You should take in count other characters that have special meanings in Regular Expressions, and translate/encode them properly. Maybe classify them. For example, these characters:
., $, ^
can be easily translated to regex patterns just encoding them with a \ before, so they will become:
\., \$, \^, ...
If you can specify what is the format of the validation patterns you are storing in the XML files, I could help you a little more, but I'm just writing this answer kind of blind ;)
Regular expressions that match certain sets of characters in a certain order are fairly simple. For example, this will match #2 (AAaaa-1111-aaa):
[A-Z]{2}[a-z]{3}-[0-9]{4}-[a-z]{3}
Breaking it down:
[A-Z]: Any character from A to Z. So any alphabetic, uppercase character.
{2}: Two of the previous item.
The rest of it works in the same way. The hyphens between things are there to match the hyphens in your expected input.
Similar to this topic.
I am trying to validate a username with the following restrictions:
Must start with a letter or number
Must be 3 to 15 characters in length
Symbols include: . - _ ( ) [ ]
Symbols cannot be adjacent, but letters and numbers can
Edit:
Letters and numbers are a-z A-Z 0-9
Been stumped for a while. I'm new to regex.
As an optimization to Mark's answer:
^(?=.{3,15}$)([A-Za-z0-9][._()\[\]-]?)*$
Explanation:
(?=.{3,15}$) Must be 3-15 characters in the string
([A-Za-z0-9][._()\[\]-]?)* The string is a sequence of alphanumerics,
each of which may be followed by a symbol
This one permits Unicode alphanumerics:
^(?=.{3,15}$)((\p{L}|\p{N})[._()\[\]-]?)*$
This one is the Unicode variant, plus uses non-capturing groups:
^(?=.{3,15}$)(?:(?:\p{L}|\p{N})[._()\[\]-]?)*$
It is not so clean to express a set of unrelated rules in a single regular expression, but it can be done by using lookaround assertions (Rubular):
#"^(?=[A-Za-z0-9])(?!.*[._()\[\]-]{2})[A-Za-z0-9._()\[\]-]{3,15}$"
Explanation:
(?=[A-Za-z0-9]) Must start with a letter or number
(?!.*[._()\[\]-]{2}) Cannot contain two consecutive symbols
[A-Za-z0-9._()\[\]-]{3,15} Must consist of between 3 to 15 allowed characters
You might want to consider if this would be easier to read and more maintable as a list of simpler regular expressions, all of which must validate successfully, or else write it in ordinary C# code.
I am checking a string with the following regexes:
[a-zA-Z0-9]+
[A-Za-z]+
For some reason, the characters:
.
-
_
are allowed to pass, why is that?
If you want to check that the complete string consists of only the wanted characters you need to anchor your regex like follows:
^[a-zA-Z0-9]+$
Otherwise every string will pass that contains a string of the allowed characters somewhere. The anchors essentially tell the regular expression engine to start looking for those characters at the start of the string and stop looking at the end of the string.
To clarify: If you just use [a-zA-Z0-9]+ as your regex, then the regex engine would rightfully reject the string -__-- as the regex doesn't match against that. There is no single character from the character class you defined.
However, with the string a-b it's different. The regular expression engine will match the first a here since that matches the expression you entered (at least one of the given characters) and won't care about the - or the b. It has done its job and successfully matched a substring according to your regular expression.
Similarly with _-abcdef- – the regex will match the substring abcdef just fine, because you didn't tell it to match only at the start or end of the string; and ignore the other characters.
So when using ^[a-zA-Z0-9]+$ as your regex you are telling the regex engine definitely that you are looking for one or more letters or digits, starting at the very beginning of the string right until the end of the string. There is no room for other characters to squeeze in or hide so this will do what you apparently want. But without the anchors, the match can be anywhere in your search string. For validation purposes you always want to use those anchors.
In regular expressions the + tells the engine to match one or more characters.
So this expression [A-Za-z]+ passes if the string contains a sequence of 1 or more alphabetic characters. The only strings that wouldn't pass are strings that contain no alphabetic characters at all.
The ^ symbol anchors the character class to the beginning of the string and the $ symbol anchors to the end of the string.
So ^[A-Za-z0-9]+ means 'match a string that begins with a sequence of one or more alphanumeric characters'. But would allow strings that include non-alphanumerics so long as those characters were not at the beginning of the string.
While ^[A-Za-z0-9]+$ means 'match a string that begins and ends with a sequence of one or more alphanumeric characters'. This is the only way to completely exclude non-alphanumerics from a string.