Need a regular expression to validate username which:
should allow trailing spaces but not spaces in between characters
must contain at least one letter,may contain letters and numbers
7-15 characters max(alphanumeric)
cannot contain special characters
underscore is allowed
Not sure how to do this. Any help is appreciated. Thank you.
This is what I was using but it allows space between characters
"(?=.*[a-zA-Z])[a-zA-Z0-9_]{1}[_a-zA-Z0-9\\s]{6,14}"
Example: user name
No spaces are allowed in user name
Try this:
foundMatch = Regex.IsMatch(subjectString, #"^(?=.*[a-z])\w{7,15}\s*$", RegexOptions.IgnoreCase);
Is also allows the use of _ since you allowed this in your attempt.
So basically I use three rules. One to check if at least one letter exists.
Another to check if the string consists only of alphas plus the _ and finally I accept trailing spaces and at least 7 with a max of 15 alpha's. You are in a good track. Keep it up and you will be answering questions here too :)
Breakdown:
"
^ # Assert position at the beginning of the string
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
)
\w # Match a single character that is a “word character” (letters, digits, etc.)
{7,15} # Between 7 and 15 times, as many times as possible, giving back as needed (greedy)
\s # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
Related
This question already has answers here:
Regular Expression for password validation
(6 answers)
Closed 1 year ago.
I'm new to React Native, and I need to implement new password requirements.
The new requirements are small and large letters or letters and at least one number or special character.
The requirement for the password to be at least eight characters.
Here is my code:
.matches(
/^(?=.*[a-z])(?=.*\d)(?=.*[\W_])[\w\W].+$/,
I think that should work:
((^|, )((?=.[a-z])|(?=.\d)|(?=.*[\W_])[\w\W]))+$.
This works. It requires at least one uppercase letter, one lowercase letter, one number, and one special character such as # or # or $, with a length of at least eight characters.
(?m)^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\W]).{8,})$
The (?m) at the beginning makes sure that the . in the regex does not match a newline.
From RegexBuddy:
^((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})$
Options: ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[\W]).{8,})»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*\d)»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single digit 0..9 «\d»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[a-z])»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character in the range between “a” and “z” «[a-z]»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[A-Z])»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character in the range between “A” and “Z” «[A-Z]»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=.*[\W])»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a “non-word character” «[\W]»
Match any single character that is not a line break character «.{8,}»
Between eight and unlimited times, as many times as possible, giving back as needed (greedy) «{8,}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
It matches
abcDefg1$
1zBA^frmb
1#Basdfadsfadsf
It does not match
abcd123
123abc
abcdEFGH
abcdEFG2
abCDeF1E
1a2bc
Assume we got address book with some unformatted data, like:
+1 (4542) 114214 111#111.org d#ghhg.com,,,,
+1 (2342) 114234 ert#nhy.sdfr.domain.org; 1#kjk.eiu.1
+7 (101) 111-222-11 abc#ert.com, def#sdf.org
+1 (102) 123532-2 some#mail.ru
+44 (301) 123 23 45 7zip#site.edu; ret#ghjj.org
I made attempts to write regex for this:
/+\d+\s(\d+)\s\d+[\d+\s | \d+ -]+/g
But i have no idea how to exclude numbers before alphabetical characters. Probably this is not even a partial solution.
Edit #1: I'm overwhelmed by all working solutions provided, many thanks to everyone. If possible, i would be grateful if you added at least some reference/explanation how to write such complicated regex.
Without knowing where you are using this regex I'd recommend using a negative lookahead.
^[+\d() -]+(?![\w#])
Demo: https://regex101.com/r/rQ6fK4/1
If you want to capture the phone number use:
^([+\d() -]+)(?![\w#])
It will be in $1 or \1, (depending on where you are using this).
That may be one of few cases, where you need a possessive quantifier.
My attempt:
\s*(\+?(\d+)\s*\(\d+\)\s+([- \d+]++(?!\#)|\d+))
The part [- \d+]++(?!\#) will stop the matching if a "#" is following. Therefore it excludes the e-mail addresses.
The phone numbers are now stored in group 1.
Edit:
Yes, the last input line doesn't match correctley. Maybe it's easier to extract the e-mail addresses with the following regex, so the phone numbers are left (and some commas, but they should be a problem to extract as well):
\s[^\# ]+\#[-\w.]+\.\w+
Here's my solution (on regex101):
\+\d+\s+\(\d+\)\s+[- \d]+(?= )
It ensures the last group of spaces, digits, and/or dashes ([- \d]+) is followed by a space ((?= )).
It cleanly captures all of your examples, without trailing spaces, and without including any part of the email addresses.
I wouldn't even try parsing the phone number.
You have a phone number, separated with a space character from one or more email addresses, which are separated by commas or semicolons. An email address always contains a #.
Find the first #. If there is none then the phone number is the trimmed string. If there is an # then find the last space before the #. The phone number is the everything up to that space, trimmed. If there is no space before the # then you have no phone number.
With phone numbers removed, you can find the emails by splitting the string around "," or ";", trimming the strings, and throwing away what doesn't contain an #.
Then find a decent number to handle phone numbers, if you need to do that beyond recording what the phone number is.
You can use see demo:
(?<phone>\+\d{1,2}\s\(\d{3,4}\)\s(?:[\d- ]+\d)(?=\s))
\s+(?<email>.*?#.*?)(?=[\s;,]|$).*?
\s+(?<email2>[\w]*?#.*?)?(?=[\s;,]|$)
Which produce:
MATCH 1
phone [4-20] `+1 (4542) 114214`
email [21-32] `111#111.org`
email2 [33-43] `d#ghhg.com`
MATCH 2
phone [52-68] `+1 (2342) 114234`
email [69-92] `ert#nhy.sdfr.domain.org`
email2 [94-105] `1#kjk.eiu.1`
MATCH 3
phone [110-129] `+7 (101) 111-222-11`
email [130-141] `abc#ert.com`
email2 [143-154] `def#sdf.org`
MATCH 4
phone [159-176] `+1 (102) 123532-2`
email [177-189] `some#mail.ru`
MATCH 5
phone [194-213] `+44 (301) 123 23 45`
email [214-227] `7zip#site.edu`
email2 [229-241] `ret#ghjj.org`
Explanation:
(?<phone>\+\d{1,2}\s\(\d{3,4}\)\s(?:[\d- ]+\d)(?=\s)) Named capturing group phone
\+ matches the character + literally
\d{1,2} match a digit [0-9]
Quantifier: {1,2} Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\( matches the character ( literally
\d{3,4} match a digit [0-9]
Quantifier: {3,4} Between 3 and 4 times, as many times as possible, giving back as needed [greedy]
\) matches the character ) literally
\s match any white space character [\r\n\t\f ]
(?:[\d- ]+\d) Non-capturing group
[\d- ]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
- a single character in the list - literally
\d match a digit [0-9]
(?=\s) Positive Lookahead - Assert that the regex below can be matched
\s match any white space character [\r\n\t\f ]
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?<email>.*?#.*?) Named capturing group email
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
# matches the character # literally
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
(?=[\s;,]|$) Positive Lookahead - Assert that the regex below can be matched
1st Alternative: [\s;,]
[\s;,] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
;, a single character in the list ;, literally
2nd Alternative: $
$ assert position at end of a line
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?<email2>[\w]*?#.*?)? Named capturing group email2
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
[\w]*? match a single character present in the list below
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
\w match any word character [a-zA-Z0-9_]
# matches the character # literally
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
(?=[\s;,]|$) Positive Lookahead - Assert that the regex below can be matched
1st Alternative: [\s;,]
[\s;,] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
;, a single character in the list ;, literally
2nd Alternative: $
$ assert position at end of a line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
x modifier: extended. Spaces and text after a # in the pattern are ignored
I try to make regular expression which helps me filter strings like
blah_blah_suffix
where suffix is any string that has length from 2 to 5 characters. So I want accept strings
blah_blah_aa
blah_blah_abcd
but discard
blah_blah_a
blah_aaa
blah_blah_aaaaaaa
I use grepl in the following way:
samples[grepl("blah_blah_.{2,5}", samples)]
but it ignores upper bound for repetition (5). So it discards strings blah_blah_a,
blah_aaa, but accepts string blah_blah_aaaaaaa.
I know there is a way to filter strings without usage of regular expression but I want to understand how to use grepl correctly.
You need to bound the expression to the start and end of the line:
^blah_blah_.{2,5}$
The ^ matches beginning of line and $ matches end of line. See a working example here: Regex101
If you want to bound the expression to the beginning and end of a string (not multi-line), use \A and \Z instead of ^ and $.
Anchors Tutorial
/^[\w]+_[\w]+_[\w]{2,5}$/
DEMO
Options: dot matches newline; case insensitive; ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]{2,5}»
Between 2 and 5 times, as many times as possible, giving back as needed (greedy) «{2,5}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
I need a regular expression that will tell if a string is in the following format. The groups of numbers must be comma delimited. Can contain a range of numbers separated by a -
300, 200-400, 1, 250-300
The groups can be in any order.
This is what I have so far, but it's not matching the entire string. It's only matching the groups of numbers.
([0-9]{1,3}-?){1,2},?
Try this one:
^(?:\d{1,3}(?:-\d{1,3})?)(?:,\s*\d{1,3}(?:-\d{1,3})?|$)+
Since you didn't specify the number ranges I leave this to you. In any case you should do math with regex :)
Explanation:
"
^ # Assert position at the beginning of the string
(?: # Match the regular expression below
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
- # Match the character “-” literally
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
)? # Between zero and one times, as many times as possible, giving back as needed (greedy)
)
(?: # Match the regular expression below
# Match either the regular expression below (attempting the next alternative only if this one fails)
, # Match the character “,” literally
\\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
- # Match the character “-” literally
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
)? # Between zero and one times, as many times as possible, giving back as needed (greedy)
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
)+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"
^(\d+(-\d+)?)(,\s*(\d+(-\d+)?))*$
This should work:
/^([0-9]{1,3}(-[0-9]{1,3})?)(,\s?([0-9]{1,3}(-[0-9]{1,3})?))*$/
You need some repetition:
(?:([0-9]{1,3}-?){1,2},?)+
To ensure that the numbers are correct, i.e. that you don't match numbers like 010, you might want to change the regex slightly. I also changed the range part of the regex, so that you don't match things like 100-200- but only 100 or 100-200, and added support for whitespaces after the comma (optional):
(?:(([1-9]{1}[0-9]{0,2})(-[1-9]{1}[0-9]{0,2})?){1,2},?\s*)+
Also, depending on what you want to capture, you might want to change the capturing brackets () to non capturing ones (?:)
UPDATE
A revised version based on the latest comments:
^\s*(?:(([1-9][0-9]{0,2})(-[1-9][0-9]{0,2})?)(?:,\s*|$))+$
([0-9-]+),\s([0-9-]+),\s([0-9-]+),\s([0-9-]+)
Try this regular expression
^(([0-9]{1,3}-?){1,2},?\s*)+$
I am trying to determine what the following pattern match criteria allows me to enter:
\s*([\w\.-]+)\s*=\s*('[^']*'|"[^"]*"|[^\s]+)
From my attempt to decipher (by looking at the regex's I do understand) it seems to say I can start with any character sequence then I must have a brace followed by alphanumerics, then another sequence followed by braces, one intial single quote, no backslashes closed by a brace ???
Sorry if I have got this completely muddled. Any help is appreciated.
Regards,
Pablo
The square brackets are character classes, and the parens are for grouping. I'm not sure what you mean by "braces".
This basically matches a name=value pair where than name consists of one or more "word", dot or hyphen characters, and the value is either a single quoted character or a double-quoted string of characters, or a bunch of non-whitespace characters. Single-quoted characters cannot contain a single quote, and double quoted strings may not contain double-quotes (both arguably minor flaws whatever syntax this is from). There's also arguably some ambiguity since the last option ("a bunch on non-whitespace characters") could match something starting with a single or double quote.
Also, zero or more whitespaces may appear around the equal sign or at the beginning (that's the \s* bits).
It's looking for strings of text which are basically
<identifier> = <value>
identifier is made up of letters, digits, '-' and '.'
value can be a single-quoted strings, double-quoted strings, or any other sequence of characters (as long as it doesn't contain a space).
So it would match lines that look like this:
foo = 1234
bar-bar= "a double-quoted string"
bar.foo-bar ='a single quoted string'
.baz =stackoverflow.com this part is ignored
Some things to note:
There's no way to put a quote inside a quoted string (such as using \" inside "...").
Anything after the quoted string is ignored.
If a quoted string isn't used for value, then everything from the first space onwards is ignored.
Whitespace is optional
RegexBuddy says:
\s*([\w\.-]+)\s*=\s*('[^']*'|"[^"]*"|[^\s]+)
Options: case insensitive
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 1 «([\w\.-]+)»
Match a single character present in the list below «[\w\.-]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A word character (letters, digits, etc.) «\w»
A . character «\.»
The character “-” «-»
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “=” literally «=»
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 2 «('[^']*'|"[^"]*"|[^\s]+)»
Match either the regular expression below (attempting the next alternative only if this one fails) «'[^']*'»
Match the character “'” literally «'»
Match any character that is NOT a “'” «[^']*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “'” literally «'»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «"[^"]*"»
Match the character “"” literally «"»
Match any character that is NOT a “"” «[^"]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “"” literally «"»
Or match regular expression number 3 below (the entire group fails if this one fails to match) «[^\s]+»
Match a single character that is a “non-whitespace character” «[^\s]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Created with RegexBuddy
Let us break \s*([\w\.-]+)\s*=\s*('[^']*'|\"[^\"]*\"|[^\s]+) apart:
\s*([\w\.-]+)\s*:
\s* means 0 or more whitespace characters
`[\w.-]+ means 1 or more of the following characters: A-Za-z0-9_.-
('[^']*'|\"[^\"]*\"|[^\s]+):
One or more characters non-' characters enclosed in ' and '.
One or more characters non-" characters enclodes in " and ".
One or more characters not containing a space
So basically, you can mostly ignore the \s*'s in trying to understand the expression, they just handle removing spacing.
Yes, you have got it completely muddled. :P For one thing, there are no braces in that regex; that word usually refers to the curly brackets: {}. That regex only contains square brackets and parentheses (aka round brackets), and they're all regex metacharacters--they aren't meant to match those characters literally. The same goes for most of the other characters.
You might find this site useful. Very good tutorial and reference site for all things regex.