I have the following regex to match this:
U$MichaelU$P#$asdqwe123P#$ - this is correct; the other two are not
U$NameU$P#$PasswordP#$
U$UserU$P#$ad2P#$
A registration is valid when:
The username is surrounded by "U$"
The username needs to be minimum 3 characters long, start with an uppercase letter, followed only by lowercase letters
The password is surrounded by "P#$"
The password needs to start with minimum 5 alphabetical letters (not including digits) and must end with a digit
My regex is
#"^(U\S)([A-z][a-z]{3,})\1(P#\S)([a-z]{5,}[^\d])([\d]+)\3$"
The problem is that it matches the first one but when I submit to the judge it passes first 2 test but the rest it breaks, could you please tell me where is my mistake.
Hello your regex must be
#"^(U\S)([A-Za-z]{3,})\1(P#\S)([A-Za-z0-9]{5,})\3$"
it works for you
Related
I'm trying to come up with a regex to deal with a general scenario to capture a number from a string where the number may have one or more non-numeric characters pre/post-fixed to it.
The number can contain zero or one decimal or comma.
If the string contains multiple "sets" of consecutive digits separated by non-digits I would like the regex to fail ("sets" is probably not the correct terminology).
As an example the following inputs would succeed with a match:
abc12.00xyz would match 12.00
0.1$ would be valid and match 0.1
.01 would be valid and match .01
123abc would be valid and match 123
abc123 would be valid and match 123
These inputs would fail to match:
abc12.00xyz322 would fail due to the second "set" of digits, 322 in this example
12t2 would fail due to having two separate "sets" of digits
I've tried many permutations and I'm not making much headway. This is the closest I've come to so far. It matches on the numbers correctly, excluding the non-digits from the match, but it includes all "sets" of numbers in the string.
([\d]*[.,])?[\d]+
Any suggestions would be appreciated.
You can use a capture group:
^[^0-9\r\n]*?([0-9]*\.?[0-9]+)[^0-9\r\n]*$
^ Start of string
[^0-9\r\n]* Optionally match any char except a digit or a newline, as few as possible
([0-9]*\.?[0-9]+) Capture group 1, match optional digits, optional comma and 1+ digits
[^0-9\r\n]* Optionally match any char except a digit or a newline
$ End of string
See a .NET regex demo (Click on the Table tab to see the capture group values)
I want to capture the first and last character within a capturing group.
My current RegEx is -
([\w\.]+)#([\w]+)\.com
For example, if there is an email address -
xyz#test.com
This is the output -
Full match 0-12 `xyz#test.com`
Group 1. 0-3 `xyz`
Group 2. 4-8 `test`
The email address can have alphanumeric and period values.
If I want to curtail the Group 1 such that it starts and ends with only alphanumeric values, how to do that?
I want to modify this capturing group -
([\w\.]+)
The required output is -
xyz.#test.com Invalid
.xyz#test.com Invalid
xy.z#test.com Valid
To tell engine match English alphanumeric characters at the start position and one before # you need to do this:
^([a-zA-Z0-9][\.a-zA-Z0-9]*[a-zA-Z0-9])#([a-zA-Z0-9]+)\.com$
Note: \w includes _ that you may not desire.
But this doesn't allow usernames with one character long. So you have to modify it a little:
^([a-zA-Z0-9]+(?:\.+[a-zA-Z0-9]+)*)#([a-zA-Z0-9]+)\.com$
Also this shouldn't be considered a good email validator. But as it seems you narrow down matching to .com TLD so I assume this is a very specific requirement otherwise it limits domain name to alphanumerics and doesn't allow many more characters that would be valid in an email address according to RFC 822. This would be enough for capturing an email address from user input:
^[^\s#]+#[^\s#]+$
Try this regex - (^[\w][\w\.\w]+[\w])#([\w]+)\.com
This works:
^([0-9a-zA-Z][a-zA-Z0-9_\.]*)(?<!\.)#([a-zA-Z0-9_]+)\.com$
Demo
Basically, it tries to match alphanumeric characters at the start, then [a-zA-Z0-9_\.] for 0 or more times. Before it reaches #, it will look behind to check if there is a dot (if it is not an alphanumeric, it's gotta be a dot).
I would like to create a custom regular expression. Which shall track that the first character of a username should be an alphabet.
Followed by alphanumeric or can have maximum one occurrence of a special character (- or _). I can check for username starts with the alphabet with this ^[a-zA-Z]+$ but not sure what to do to check at most one occurrence of a special character. Any ideas are welcome.
Thanks
From what I understood of your post, you want the following to match.
a-afdsafd
aafdsafd
aafdsa_fd
aafdsa-fd
aAfdsa-FD
And the following to not match:
aa-dsa-fd
aa-dsa_fd
-afdsafd
_afdsafd
Try /^[a-z](?:(?![a-z]+[\-_])[\-_])?[a-z]+(?:(?<![a-z]+[\-_])[\-_]?)[a-z]+?$/i
The i modifier enables case-insensitive matching.
The ^ and $ anchors ensure that the entire string matches our regex.
[a-z] checks that the first character is an alphabet.
(?:(?![a-z]+[\-_])[\-_])?) looks ahead to check that there is no "special character" used later and if there is none, we optionally match one special character.
[a-z]+ Match one or more alphabets.
(?:(?<![a-z]+[\-_])[\-_]?) does the same thing as 4 except it looks behind.
[a-z]+? Optionally match one or more alphabets.
https://regexr.com/3t86l
Edit: I noticed that aAfdsaFd_ should also match. The above does not match this. Slightly modifying #Wiktor Stribiżew's comment, ^[a-zA-Z][a-zA-Z0-9]*(?:[-_][a-zA-Z0-9]*)?$ seems to work fine with all cases. That's cleaner and more efficient. All credit to #Wiktor Stribiżew.
You could match an upper or lowercase character from the start of the string ^[a-zA-Z], match zero or more times alphanumeric [a-zA-Z0-9]* followed by an optional hyphen or underscore [-_]?.
At the end match zero or more times alphanumeric [a-zA-Z0-9]*$ until the end of the string.
^[a-zA-Z][a-zA-Z0-9]*[-_]?[a-zA-Z0-9]*$
This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 9 years ago.
I am trying to replace our password validation with a simple RegEx in my asp.net project which uses regularexpression validator.
Here is the password restrictions:
Password should be of minimum 6 chars in length and maximum 15
It should have at least one letter (any case)
It should have at least one digit
It should have at least one special character.
I am n00b at regex and this is the only type of question where i ask for spoon feeding ;)
I tried below regex but it fails in few cases.
string re1="([a-z])"; // Any Single Word Character (Not Whitespace) 1
string re2=".*?"; // Non-greedy match on filler
string re3="."; // Uninteresting: c
string re4=".*?"; // Non-greedy match on filler
string re5="."; // Uninteresting: c
string re6=".*?"; // Non-greedy match on filler
string re7="(.)"; // Any Single Character 1
string re8="(\\d)"; // Any Single Digit 1
Regex r = new Regex(re1+re2+re3+re4+re5+re6+re7+re8,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt)
Use a pattern like this:
^(?=.*[a-z])(?=.*[0-9])(?=.*[...]).{6,15}$
Where you can replace the [...] with whatever characters you want to accept as 'special characters'.
To break this down a bit:
The start (^) and end ($) anchors ensure there are no leading or trailing characters in the input. This is necessary to ensure the maximum length is enforced.
The .{6,15} bit matches 6 to 15 of any character.
The (?=...) is a lookahead. It ensures that the position being matched is followed by whatever pattern appears inside.
The .*[a-z] means any number of characters followed by a single Latin letter.
Similarly, .*[0-9] matches any number of characters followed by a decimal digit, and .*[...] matches any number of characters followed by one of your 'special characters'.
So collectively, the chain of (?=.*[a-z])(?=.*[0-9])(?=.*[...]) means that all three of these patterns must be present within the following string, in any order.
I'm new to this also and I found a post on validating filenames that should work the same.
if(preg_match('/^[a-z0-9-_]+$/',$file_name))
{
echo 'good filename';
}
else
{
echo ' The file name can only contain "a-z", "0-9", "_", and "-"';
}
This checks to see if it contains certain characters which you can use to detect numbers and letters in a password. Also, if I were you I would also compare the password against a list of common passwords such as "password" "password123" etc.
Can any one provide me regx validator which validate the following;
Password must be an alphanumeric password i.e. at least 1 number and at least 1 alphabet.
I tried the following, but it did not work.
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9])$
You are almost there, you just need to add a quantifier for your last expression and it will work fine. So, it should be something like this
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$.
Code will look like this
Regex regexObj = new Regex(#"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$");
boolean foundMatch = regexObj.IsMatch(passwordString);
You are currently matching a single digit or a letter as [a-zA-Z0-9] matches a single alphanum character. Read about character classes([]) here http://www.regular-expressions.info/charclass.html