TSQL, ensure at least one character appears before # sign - sql

In TSQL, if I'm searching for valid .com email addresses, I need to make sure there's an # sign, it ends in .com, and there's at least one character before and after the #.
SELECT * FROM CUSTOMER WHERE [EMAIL] LIKE '%#%.com';"
I don't believe the above query would satisfy the requirement of ensuring that there is at least one character before and after the # sign (and before the .com sign in the second case). How can I adjust the wildcards in this example to mean "any sequence of 1 or more characters" where each % sign is?

Use the underscore character to match a mandatory single character in conjunction with % to match optional additional ones.
LIKE '_%#_%.com';"

SELECT * FROM CUSTOMER WHERE EMAIL LIKE _%#%_.com;
This may satisfy your requirement as _ is used to indicate a compulsory character.

Related

onelogin username and email regex

Where can I find the username and email requirements/regex on onelogin's docs? I have looked everywhere but cannot find it
In general it is pretty accepting. Although customers can add rules to make their acceptance criteria more strict.
email = [^#\s]+#[a-Z0-9]+.[a-Z0-9]+
this means:
1. one or more characters that aren't the # sign or whitespace
2. the # sign
3. one or more characters a-Z or 0-9
4. the period '.'
5. one or more characters a-Z or 0-9
username = .*
this means basically any character but new lines
both must be unique within the customers account though.

Regex like telephone number on Hive without prefix (+01)

We have a problem with a regular expression on hive.
We need to exclude the numbers with +37 or 0037 at the beginning of the record (it could be a false result on the regex like) and without letters or space.
We're trying with this one:
regexp_like(tel_number,'^\+37|^0037+[a-zA-ZÀÈÌÒÙ ]')
but it doesn't work.
Edit: we want it to come out from the select as true (correct number) or false.
To exclude numbers which start with +01 0r +001 or +0001 and having only digits without spaces or letters:
... WHERE tel_number NOT rlike '^\\+0{1,3}1\\d+$'
Special characters like + and character classes like \d in Hive should be escaped using double-slash: \\+ and \\d.
The general question is, if you want to describe a malformed telephone number in your regex and exclude everything that matches the pattern or if you want to describe a well-formed telephone number and include everything that matches the pattern.
Which way to go, depends on your scenario. From what I understand of your requirements, adding "not starting with 0037 or +37" as a condition to a well-formed telephone number could be a good approach.
The pattern would be like this:
Your number can start with either + or 00: ^(\+|00)
It cannot be followed by a 37 which in regex can be expressed by the following set of alternatives:
a. It is followed first by a 3 then by anything but 7: 3[0-689]
b. It is followed first by anything but 3 then by any number: [0-24-9]\d
After that there is a sequence of numbers of undefined length (at least one) until the end of the string: \d+$
Putting everything together:
^(\+|00)(3[0-689]|[0-24-9]\d)\d+$
You can play with this regex here and see if this fits your needs: https://regex101.com/r/KK5rjE/3
Note: as leftjoin has pointed out: To use this regex in hive you might need to additionally escape the backslashes \ in the pattern.
You can use
regexp_like(tel_number,'^(?!\\+37|0037)\\+?\\d+$')
See the regex demo. Details:
^ - start of string
(?!\+37|0037) - a negative lookahead that fails the match if there is +37 or 0037 immediately to the right of the current location
\+? - an optional + sign
\d+ - one or more digits
$ - end of string.

regex not working correctly when the test is fine

For my database, I have a list of company numbers where some of them start with two letters. I have created a regex which should eliminate these from a query and according to my tests, it should. But when executed, the result still contains the numbers with letters.
Here is my regex, which I've tested on https://www.regexpal.com
([^A-Z+|a-z+].*)
I've tested it against numerous variations such as SC08093, ZC000191 and NI232312 which shouldn't match and don't in the tests, which is fine.
My sql query looks like;
SELECT companyNumber FROM company_data
WHERE companyNumber ~ '([^A-Z+|a-z+].*)' order by companyNumber desc
To summerise, strings like SC08093 should not match as they start with letters.
I've read through the documentation for postgres but I couldn't seem to find anything regarding this. I'm not sure what I'm missing here. Thanks.
The ~ '([^A-Z+|a-z+].*)' does not work because this is a [^A-Z+|a-z+].* regex matching operation that returns true even upon a partial match (regex matching operation does not require full string match, and thus the pattern can match anywhere in the string). [^A-Z+|a-z+].* matches a letter from A to Z, +,|or a letter fromatoz`, and then any amount of any zero or more chars, anywhere inside a string.
You may use
WHERE companyNumber NOT SIMILAR TO '[A-Za-z]{2}%'
See the online demo
Here, NOT SIMILAR TO returns the inverse result of the SIMILAR TO operation. This SIMILAR TO operator accepts patterns that are almost regex patterns, but are also like regular wildcard patterns. NOT SIMILAR TO '[A-Za-z]{2}%' means all records that start with two ASCII letters ([A-Za-z]{2}) and having anything after (%) are NOT returned and all others will be returned. Note that SIMILAR TO requires a full string match, same as LIKE.
Your pattern: [^A-Z+|a-z+].* means "a string where at least some characters are not A-Z" - to extend that to the whole string you would need to use an anchored regex as shown by S-Man (the group defined with (..) isn't really necessary btw)
I would probably use a regex that specifies want the valid pattern is and then use !~ instead.
where company !~ '^[0-9].*$'
^[0-9].*$ means "only consists of numbers" and the !~ means "does not match"
or
where not (company ~ '^[0-9].*$')
Not start with a letter could be done with
WHERE company ~ '^[^A-Za-z].*'
demo: db<>fiddle
The first ^ marks the beginning. The [^A-Za-z] says "no letter" (including small and capital letters).
Edit: Changed [A-z] into the more precise [A-Za-z] (Why is this regex allowing a caret?)

Devise gem - Do not allow special character in password

I have used devise gem in rails application. password format for validation I found is
PASSWORD_FORMAT_USED_CURRENTLY = /\A
(?=.{10,}) # Must contain 10 or more characters
(?=.*\d) # Must contain a digit
(?=.*[a-z]) # Must contain a lower case character
(?=.*[A-Z]) # Must contain an upper case character
(?=.*[[:^alnum:]]) # Must contain a symbol
/x
It works fine but I want two requirements to be fulfilled for my password
1) password must contain at least 6 letters ( it can be capital letters, small letters, digits or a combination of all ).
PASSWORD_FORMAT = /\A
(?=.{6,}) # Must contain 6 or more characters
/x
2) the second criteria is that the password should not contain any special characters.
I don't know how to achieve this
what I found is only that how can I make compulsory the presence of special characters but not vice versa.
Thanks in advance
The second regex itself will handle both the cases.
PASSWORD_FORMAT = /\A
(?=.{6,}) # Must contain 6 or more characters
(?=.*\w) # Must contain capital letters, small letters, digits or a combination of all. Must not contain any special characters.
/x

Password Validation Regular Expression - how to include special character?

#"^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{6,}$"
I am using this regular expression for password validation which gives one upper case, one lowercase and a number. But what I want is a special character in it but it should optional but above mentioned must be mandatory.
This will allow these special characters: - (hyphen), * (asterisk) and _ (underscore).
^(?=[-_*]*)(?=.*[0-9]+.*)(?=.[a-zA-Z]+.)[-*_0-9a-zA-Z]{6,}$
If you want to add your own special characters, add them into this part of the regex [-*_0-9a-zA-Z] (inside the square bracket)