Regex - Match only if input does not contain all letters - regex

I understand the regex used to match if it contains all letters is ^[a-zA-Z]+$ so I thought a negation of this regex would be the answer and tried ^(?!^[a-zA-Z])+$ but it doesn't seem to work.
To provide some context, I'm creating a basic form using SurveyJS form creator that accepts custom validation only via regex. A certain form input should allow users to input anything and only throw an error if the user only fill in letters.

You can use
^(?![a-zA-Z]+$).*
The negative lookahead ensures the whole line does not only contain a-zA-Z till the $ (end of line - thats why its included inside the lookahead - not outside of it) and the .* afterwards would match anything that passes the negative lookahead.
Demo: https://regex101.com/r/QuC2SQ/1

Related

Regex Email validation with some special cases [duplicate]

I am trying to make a regex match which is discarding the lookahead completely.
\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
This is the match and this is my regex101 test.
But when an email starts with - or _ or . it should not match it completely, not just remove the initial symbols. Any ideas are welcome, I've been searching for the past half an hour, but can't figure out how to drop the entire email when it starts with those symbols.
You can use the word boundary near # with a negative lookbehind to check if we are at the beginning of a string or right after a whitespace, then check if the 1st symbol is not inside the unwanted class [^\s\-_.]:
(?<=^|\s)[^\s\-_.]\w*(?:[-+.]\w+)*\b#\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*
See demo
List of matches:
support#github.com
s.miller#mit.edu
j.hopking#york.ac.uk
steve.parker#soft.de
info#company-hotels.org
kiki#hotmail.co.uk
no-reply#github.com
s.peterson#mail.uu.net
info-bg#software-software.software.academy
Additional notes on usage and alternative notation
Note that it is best practice to use as few escaped chars as possible in the regex, so, the [^\s\-_.] can be written as [^\s_.-], with the hyphen at the end of the character class still denoting a literal hyphen, not a range. Also, if you plan to use the pattern in other regex engines, you might find difficulties with the alternation in the lookbehind, and then you can replace (?<=\s|^) with the equivalent (?<!\S). See this regex:
(?<!\S)[^\s_.-]\w*(?:[-+.]\w+)*\b#\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*
And last but not least, if you need to use it in JavaScript or other languages not supporting lookarounds, replace the (?<!\S)/(?<=\s|^) with a (non)capturing group (\s|^), wrap the whole email pattern part with another set of capturing parentheses and use the language means to grab Group 1 contents:
(\s|^)([^\s_.-]\w*(?:[-+.]\w+)*\b#\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*)
See the regex demo.
I use this for multiple email addresses, separate with ‘;':
([A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4};)*
For a single mail:
[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}

How to format allowence of multiple whitespaces between characters in Regex more compact?

I came up with this regEx to check if a IBAN is entered correctly into a field which also let's the user enter up to 4 whitespaces between character without causing an error.
^\s?\s?\s?\s?N\s?\s?\s?\s?\s?O\s?\s?\s?\s?([0-9a-zA-Z]\s?\s?\s?\s?){13}$
It works perfectly, but I want to get rid of the "\s?\s?\s?\s?" and format it more compact, I've tried [\s?]{4} but that doesn't work.
What's the correct way to shorten this up?
The system I work with doesn't allow me to use any Javascript, I can only put pure regEx definitions to control entry into the field.
thank you
You can shorten the repeating \s parts using a quantifier {0,4} to match 0-4 times a whitespace char and add an anchor $ to assert the end of the string to prevent a partial match.
If you don't need that value of the capturing group afterwards, you could make it non capturing (?: instead.
^\s{0,4}N\s{0,4}O\s{0,4}(?:[0-9a-zA-Z]\s{0,4}){13}$
Regex demo
If you don't want to match a newline, you could use [^\S\r\n]{0,4} instead of \s{0,4} but that would defeat the purpose of making the pattern smaller.

Javascript Regex exclude matches that contain keywords

I'm trying to do a single javascript regex that matches email addresses that start with lcp_ but ignore any matches that also contain the word auto at any position.
I've tried a few things with no luck
/^lcp[._-](?!auto)/gi
The goal is following
lcp_land#blah.com - match
lcp_land_auto#blah.com - no match
Thanks
You can you use a "tempered greedy token" which basically means you are checking a negative lookahead with each repetition of the sub-pattern, so as to exclude the illegal string at any position, something like this for example:
\blcp_(?:(?!auto)\S)+(?=\s)
https://regex101.com/r/I0xisv/2

Regex - Issues with using Boundary to excluding words

In my authentification web site, I'm using regex to control a blacklist password. (example of blacklisted password : 12345678, 123456789, baseball, football)
I would like to add new regex rule (using boundary), which will exclude words (black listed password). I have read some similar questions on StackOverflow and tried to declare it with something like this:
^(?!\b12345678\b|\b123456789\b|\bbaseball\b|\bfootball\b|\bsuperman\b).*$
this regex doesn't match the words above, it's correct. For exemple "Baseball" with a letter, number or special character (before or after the "baseball") must match.
But "baseball!" doesn't match contrary to "!baseball". Can you give me some advices how to do it?
But "baseball!" doesn't match contrary to "!baseball"…
baseball! doesn't match because your pattern doesn't allow baseball at the beginning (^ followed by a negative lookahead for baseball).
!baseball in contrast matches because ! is placed at the beginning, and the negative lookahead is done only there, not aft.
One could think of putting the .* at different places, but that will lead to nothing.
Just include the anchors ^ $ in the lookahead:
(?!^(12345678|123456789|baseball|football|superman)$)^.*$
(in fact, we could even drop the initial ^).

validate a path structure ending in a filename.xml

I'm having trouble with a regular expression. Hope someone can help or point in right direction. Essentially I've to validate a path structure.
The rules for valid input to my method are:
(including the forward slash) starts with /usersname/
must only be one occurrence of /usersname/
after the one occurrence of /usersname/ there must only be one [aphanumeric-_ space].xml
So for example, the following are valid input into my method:
/norrisc/thesf6457.xml
/norrisc/thess63-57.xml
/norrisc/thqsf64-57 gdhy.xml
/norrisc/ase45tg_3.xml
.. and the following are *in*valid input into method:
/norrisc/anotherFolder/thesf6457.xml
/norrisc/norrisc/thess63-57.xml
/norrisc/norrisc/thess63-57.txt
/norrisc/norrisc/thess63-57
/norrisc/thqsf64-57 gdhy.xml/kjhfsd.xml
My efforts (to no avail) so far are..
\b[/username/]{1}^[a-zA-Z0-9_\\s-]+$\.xml
^[/username/]{1}[a-zA-Z0-9_\\s-]+$\.xml{1}
\b/username/{1}[a-zA-Z0-9_\\s-]+$\.xml{1}
Hope someone can help.. ?
Thanks v much
This worked for me on your test cases:
^\/username\/(?!.*\/)(\w|\s|-)+\.xml$
where username, obviously, is the literal username or a variable containing it.
Breaking that down ...
^ - start of string
\/username\/ - literal username enclosed by /
(?!.*\/) - negative lookahead: ensures the rest of the string does not contain another /
(\w|\s|-)+ - one or more letters, digits, spaces, _, or -
\.xml - literal .xml
$ - end of string
If you're unfamiliar with lookaheads, the (?=) structure lets you match using a zero-width assertion. For example, (?=a) would attempt to recognize an a character but will NOT include it as part of the match (that's what "zero-width" means— ^ and $ are other examples of zero-width assertions). This is called a positive lookahead and lets you "skip over" characters in a sense.
(?!) does the same thing, but checks if the specified pattern does not exist. It's called a negative lookahead. So in the regex above, (?!.*\/) looks for the .*\/ pattern, which means "any or zero character(s) followed by a slash". If it finds this, such as in the string /username/another_username/whatever.xml, the match will NOT succeed (because the lookahead is negative).