Regular expression with alpha, hyphens, underscore and dots characters - regex

I've wrote a regex:
/^[a-zA-Z\-\_\. ]{2,60}$/
It does work fine-ish however it allows --- or ___ or ... or even -_. to be entered as an input (without 2 alpha at least) and I don't want that. For instance I can have -aa, a-a, aa--- (similarly for the other characters).
The requirement is that there should be at least 2 alpha in the string, and the hyphens and the other 2 non-alpha symbols mentioned can be anywhere inside the string.

Use
/^(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/
See the regex demo
Details:
^ - start of string
(?=(?:[^a-zA-Z]*[a-zA-Z]){2}) - at least 2 alpha chars in the string (that is, there must be exactly 2 consecutive occurrences of:
[^a-zA-Z]* - zero or more chars other than ASCII letters
[a-zA-Z] - an ASCII letter)
[-_. a-zA-Z]{2,60} - 2 to 60 occurrences of allowed chars
$ - end of string
Note you do not need to escape - if it is at the start/end of the character class. _ is a word char, no need escaping it anywhere. The . does not need escaping inside a character class.
To tell the regex engine to limit ., _ and - chars to max 10 in the string, add (?!(?:[^._-]*[._-]){11}) negative lookahead after ^ anchor:
/^(?!(?:[^._-]*[._-]){11})(?=(?:[^a-zA-Z]*[a-zA-Z]){2})[-_. a-zA-Z]{2,60}$/

Related

Prevent repeating special character and by specific count using RegEx For Email

How can I use RegEx to test for the following pattern:
String length doesn't matter.
The special character sign (-) should not be repeated consecutively.
The special character sign (-) should not occur more than twice in the entire string and (.) sign not to be present more than two times before mail's domain
Sample Cases:
Match:
Jessica-Parker#gmail.com
Jessica-Parker#gmail.com
Jessica-Parker-Smith#gmail.com
Jessica.Parker-Smith#gmail.com
Jessica.Parker.Smith#gmail.com
Unmatch:
Jessica--Parker#gmail.com
Jeff--smith-Parker#gmail.com
Jessica-Parker--#gmail.com
Jessica-Parker-#gmail.com
Jessica.P.Jane.Smith#gmail.com
Jessica..P.Jane.Smith#gmail.com
Struggling with third part,I figured out the First two part as:
^(?!.*(-)\1{2})(\w[\w\.-]*#[a-zA-Z]+\.[a-zA-Z]+)\b
You may use
^(?![^#]*([-.])\1)(?![^#]*([-.])(?:[^#]*\2){2})\w(?:[\w.-]*\w)?#(?:[a-zA-Z]+\.)+[a-zA-Z]+$
See the regex demo.
Details
^ - start of string
(?![^#]*([-.])\1) - no double hyphen/dot anywhere after 0 or more chars other than # are allowed
(?![^#]*([-.])(?:[^#]*\2){2}) - no 3 or more -/. chars after any 0 or more chars other than # are allowed
\w - a word char
(?:[\w.-]*\w)? - an optional sequence of 0 or more word, . or - chars followed with a word char
# - a # char
(?:[a-zA-Z]+\.)+ - 1 or more occurrences of 1+ letters followed with a dot
[a-zA-Z]+ - 1+ letters
$ - end of string.

Regular Expression containing letters and spaces in specified fashion

I am working on a text processing Api in java. I need to match the strings which are:
At least 8 characters in length.
Should only contain uppercase letters, lowercase letters or spaces.
Spaces should not be present in between the letters. They can however be leading or trailing. The String can also contain only spaces which are at least 8.
Regular expression which I tried but failed:
^\s*[a-zA-Z]{8,}\s*$
Demo of my tries in here.
Any help will be welcomed.
You can use the below regex to achieve your result:
^(?=.{8,}) *[a-zA-Z]* *$
Explanation of the above regex:
^ - denotes start of the test String.
(?=) - Positive lookahead.
.{8,} - any character other than newline with length at least 8.
* - 0 or more spaces in order to match the leading spaces.(\s is avoided)
[a-zA-Z]* - 0 or more letters (uppercase or lowercase). (You can use [a-z]* along with i(case insensitive) flag. Although, there will be no effect on performance.)
* - 0 or more spaces in order to match the trailing spaces.(\s is avoided)
$ - denotes end of the test String.
Above regex demo.

Negating a complex regex containing three parts

I need a regex which is matched when the string doesn't have both lowercase and uppercase letters.
If the string has only lowercase letters -> should be matched
If the string has only uppercase letters -> should be matched
If the string has only digits or special characters -> should be matched
For example
abc, ABC, 123, abc123, ABC123&^ - should match
AbC, A12b, AB^%12c - should not match
Basically I need an inverse/negation of the following regex:
^(?=.*[a-z])(?=.*[A-Z]).+$
Does not sound like any lookarounds would be needed.
Either match only characters that are not a-z or only characters, that are not A-Z.
^(?:[^a-z]+|[^A-Z]+)$
See this demo at regex101 (used + for one or more)
You may use
^(?!.*[A-Z].*[a-z])(?!.*[a-z].*[A-Z])\S+$
Or
^(?=(?:[^a-z]+|[^A-Z]+)$).*$
See the regex demo #1 and regex demo #2
A lookaround solution like this can be used in more complex scenarios, when you need to apply more restrictions on the pattern. Else, consider a non-lookaround solution.
Details
^ - start of string
(?!.*[A-Z].*[a-z]) - no uppercase followed with a lowercase letter
(?!.*[a-z].*[A-Z]) - no lowercase letter followed with an uppercase one
(?=(?:[^a-z]+|[^A-Z]+)$) - a positive lookahead that requires 1 or more characters other than lowercase ASCII letters ([^a-z]+) to the end of the string, or 1 or more characters other than uppercase ASCII letters ([^A-Z]+) to the end of the string
.+ - 1+ chars other than line break chars
$ - end of string.
You can use this regex
^(([A-Z0-9?&%^](?![a-z]))+|([a-z0-9?&%^](?![A-Z]))+)$
You can test more cases here.
I've only added the characcter ?&%^ as possible character, but you could add which ever you like.
I would go with:
^(?:[^a-z]+?|[^A-Z]+?)$
It translates to "If the entire string is composed of non-lowercase letters or non-uppercase letters then match the string."
Lazy quantifiers +? are used so that the end-string $ anchor is obeyed when the multiline flag is enabled. If you're only validating a single-line string the you can simply use + without the question mark.
If you have a whitelist of specific allowed special chars then change [^A-Z] into [A-Z0-9()_+=-] and list the allowed special chars.
https://regex101.com/r/Wg6tLn/1

regex is allowing character while using \s

How do I write a regular expression for checking if the entered value are all white space or empty or digits. In all other cases it will return false. I tried
\s*|\d+ which is allowing character values too.
Your regex \s*|\d+ just matches 0 or more whitespace or 1 or more digits anywhere inside an input string and it may contain whitespace mixed with digits and other characters.
You can use the following regex:
^(?:\s+|\d+)?$
See demo
The regex matches:
^ - start of string
(?:\s+|\d+)? - 1 or 0 occurrences (due to (?:...)?) of...
\s+ - 1 or more whitespace symbols or
\d+ - 1 or more digits
$ - end of string
In plain human words,
^....$ - make sure we require a full string match, any partial matches inside a string are not allowed
(?:...)? - all the texts matched with the subpatterns inside this group are optional, thus allowing an empty string match (i.e. return true if the string is empty).

Regex to find 2 the same chars at the end of a string

I have to find a regex with following rules.
contains 8 to 20 chars (capital or normal).
contains no whitespace chars.
can't start with a number(0-9) or underscore (_).
at the end of the string it hase to be 2 of the same char.
must contain at least 1 number.
OK:
+234567899
a_1de*Gg
xy1Me*__
!41deF_hij2lMnopq3ss
C234567890123$^67800
*5555555
sDF564zer""
!!!!!!!!!4!!!!!!!!!!
abcdefghijklmnopq9ss
Not OK:
has more or less then 8-20 chars:
a_1+Eff
B41def_hIJ2lmnopq3stt
abCDefghijklmnopqrss5
has whitespace chars:
A_4 e*gg
starts with a number or underscore:
__1+Eff
841DEf_hij2lmnopq3stt
ends with two different chars:
a_1+eFg
b41DEf_hij2lmnopq3st
contains no numbers:
abCDefghijklmnopqrss
abcdef+++dF
!!!!!!!!!!!!!!!!!!!!
So far I have this
((?m:[^0-9_]^(?=.*[0-9])\S{8,20}$))
But I can't seem to figure out the 2 same chars at the end?
The following will work in most regex flavors (PCRE, Python, PHP, JavaScript):
/^(?=\S{8,20}$)(?=\D*\d)(?![0-9_]).{6,18}?(.)\1$/i
Demo with unit tests against your sample cases
Explanation:
/ delimiter
^ start of string
(?=\S{8,20}$) followed by 8-20 non-whitespace characters
(?=\D*\d) contains a digit
(?![0-9_]) can't start with a number or underscore
.{6,18}? non-greedy character match (moves us from the start of the string toward the end)
(.)\1 match any character, followed by the same character again
$ end of the string
/ delimiter
i flag: case-insensitive (required to see Gg, for example, as the same character twice)
The following one should suit your needs:
^(?=.*\d)[\D\S]\S{5,17}(\S)\1$
Visualization by Debuggex
Demo on regex101