what is Mysql query REGEXP to call this?
#text
#user_name
#4ll_r1ght
#last2
#_last1
#and1more_
SELECT * FROM users WHERE username REGEXP '^\#[0-9a-zA-Z_]+$'
Will select users with usernames starting with # and consisting of only alphanumeric characters (at least one).
I hope you are looking for regular expression for username with specified characters.
Try below :
^[a-zA-Z0-9._-]+#
This part of the expression validates the ‘username’ section of the email address. The hat sign (^) at the beginning of the expression represents the start of the string.
If we didn’t include sign (^), then someone could key in anything they wanted before the email address and it would still validate.
Here, we are allowing the letters a-z, A-Z, the numbers 0-9, and the symbols underscore (_), period (.), and dash (-). You can add/remove them according to your needs.
Related
I needed an MySQL Query to find all rows which contained a text pattern like
It'S hot
(an apostrophe followed by an uppercase letter), made by mistake (a typo), when the typist held the shift key too long while typing fast.
SELECT artist, title
FROM songs
WHERE BINARY title REGEXP '^.*\'[A-Z]+'
BINARY forces case sensitivity,
^ starts at beginning of data field,
.* matches any character(s)
\\' escapes a single quote
[A-Z] matches uppercase A thru Z
\+ matches anything after it, even nothing i.e. end of line
I want to return rows where certain fields follow a particular pattern such as whether a particular character in a string is a letter or number. To test it out, I want to return fields where the first letter is any letter. I used this code.
SELECT * FROM sales WHERE `customerfirstname` like '[a-z]%';
It returns nothing. So I would think that the criteria is the first character is a letter and then any following characters do not matter.
The following code works, but limits rows where the first character is an a.
SELECT * FROM sales WHERE `customerfirstname` like 'a%';
Am I not understanding pattern matching? Isn't it [a-z] or [A-Z] or [0-9] for any letter or number?
Also if I wanted to run this test on the second character in a string, wouldn't I use
SELECT * FROM `sales` WHERE `customerfirstname` like '_[a-z]%'
This is for SQL and MySQL. I am doing this in phpmyadmin.
You want to use regular expressions:
SELECT s.*
FROM sales s
WHERE s.customerfirstname REGEXP '^[a-zA-Z]';
This can be achieved with a regular expression.
SELECT * FROM sales WHERE REGEXP_LIKE(customerfirstname, '^[[:alpha:]]');
^ denotes the start of the string, while the [:alpha] character class matches any alphabetic character.
Just in case, here are a few others character classes that you may find useful :
alnum : dlphanumeric characters
digit: digit characters
lower : lowercase alphabetic characters
upper: uppercase alphabetic characters
See the mysql regexp docs for many more...
I am trying to create a regex which identifies an email between a long string.
The below regex works fine for email :
^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
But I need to create a regex such that this should return true :
SELECT 'hfdjj abc#enmail.com jkdfk' REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$';
I want to have any number of characters before and after the email.
Thanks,
Aman
If you're happy with the matching of the email, simply removing the ^ and $ characters from the start and end should suffice.
^ matches the beginning of the string.
$ matches the end of the string.
Between them, they are what's telling MySQL to not match where there are things either side of the email address.
I'm using the below UPDATE statement to update a flag that confirms if a Code is correctly formatted, note the code can be anywhere in 'RefCode'.
This works in Excel, but I understand MySQL REGEX is a little different to standard REGEX:
UPDATE tblRequests
SET flagIsRefCodeOK= (RefCode REGEXP '^[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}$')
WHERE DataSetID=11;
In a nut shell, it should be true/[1] if the field contains ddda999d-99de-999e-999e-9b9bf9999999:
8 alphanumeric characters
A SINGLE DASH
4 alphanumeric characters
A SINGLE DASH
4 alphanumeric characters
A SINGLE DASH
4 alphanumeric characters
A SINGLE DASH
12 alphanumeric characters
Would appreciate any assistance with this.
Thnx
In MySQL you cannot use (?: non-capture groups )
Do something like this:
UPDATE mytable
SET flag = `1`
WHERE mycolumn REGEXP "^[[:alnum:]]{8}-([[:alnum:]]{4}-){3}[[:alnum:]]{12}$"
Note that the POSIX class [:alnum:] matches ASCII letters a-z, A-Z and digits 0-9
In MySQL, when searching for a keyword in a text field where only "whole word match" is desired, one could use REGEXP and the [[:<:]] and [[:>:]] word-boundary markers:
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]word[[:>:]]"
For example, when we want to find all text fields containing "europe", using
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]europe[[:>:]]"
would return "europe map", but not "european union".
However, when the target matching words contains "dot characters", like "u.s.", how should I submit a proper query? I tried the following queries but none of them look correct.
1.
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u.s.[[:>:]]"
2.
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u[.]s[.][[:>:]]"
3.
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u\.s\.[[:>:]]"
When using double backslash to escape special characters, as suggested by d'alar'cop, it returns empty, even though there are something like "u.s. congress" in the table
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u\\.s\\.[[:>:]]"
Any suggestion is appreciated!
This regex does what you want:
SELECT name
FROM tbl_name
WHERE name REGEXP '([[:blank:][:punct:]]|^)u[.]s[.]([[:punct:][:blank:]]|$)'
This matches u.s. when preceeded by:
a blank (space, tab etc)
punctuation (comma, bracket etc)
nothing (ie at start of line)
and followed by:
a blank (space, tab etc)
punctuation (comma, bracket etc)
nothing (ie at end of line)
See an SQLFiddle with edge cases covering above points.
The fundamental issue with your predicates is that . is a non-word character, and any non-word character will cause the word boundary test to fail if they follow a start test or precede an end test. You can see the behavior here.
To further complicate the issue, the flavor of regular expressions used by MySQL is very limited. According to Regular-Expressions.info, MySQL uses POSIX-ERE which if you read the chart at the bottom Regular Expression Flavor Comparisons has very few capabilities where compared to other flavors.
To solve your problem you must create a new regular expression that will replace the functionality of the word boundary so that it will allow non-word characters to be part of the boundary. I came up with the follow Regular Expression:
(^|[^[:alnum:]_])YOUR_TEXT_HERE($|[^[:alnum:]_])
This is equivalent to the standard regular expression below:
(^|[^a-zA-Z0-9_])YOUR_TEXT_HERE($|[^a-zA-Z0-9_])
The regex searches for non-words characters or string boundaries at the start and end of the text. (^|[^[:alnum:]_]) matches either start of string, an alpha-numeric character, or an underscore. The ending pattern is similar except it matches the end of a string instead of the start.
The pattern was designed to best match the definition of word boundaries from Regular Expressions in the MySQL manual:
[Boundaries] match the beginning and end of words, respectively. A
word is a sequence of word characters that is not preceded by or
followed by word characters. A word character is an alphanumeric
character in the alnum class or an underscore.
Test Results
Using the regex above, I came up with a scenario where I test a string that contains non-word characters at the start and end - .u.s.. I tried to come up with a reasonable set of test items. You can see the results at
SQLFiddle.
Test Data
test string not present: 'no match'
missing .'s: 'no us match'
missing last .: 'no u.s match'
missing first .: 'no us. match'
test start boundary word character: 'no.u.s.match'
test end boundary word character: 'no .u.s.match'
test boundaries word character: 'no.u.s.match'
test basic success case: 'yes .u.s. match'
test start boundary non-word character: 'yes !.u.s. match'
test end boundary non-word character: 'yes .u.s.! match'
test boundaries non-word character: 'yes !.u.s.! match'
test start of line: '.u.s.! yes match'
test end of line: 'yes match .u.s.'
Query
SELECT *
FROM TestRegex
WHERE name REGEXP '(^|[^[:alnum:]_])[.]u[.]s[.]($|[^[:alnum:]_])';
SQLFiddle
Conclusion
All the positive cases were returned and none of the negative ones => All test cases succeeded.
You can use [.] for the period character instead of \\. which I find to be somewhat more readable in the context of a SQL expression.
You can adjust the sets used to define the boundary to be more or less restrictive depending on your desires. For example you can restrict some non-word characters as well: [^a-zA-Z_0-9.!?#$].
Working example here: http://www.sqlfiddle.com/#!2/5aa90d/9/0
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u\\.s\\.([^[:alnum:]]|$)"
Basically saying that u.s. must be followed by anything that isn't an alphanumeric character, or the end of the string.
You could change [:alnum:] to [:alpha:] to include results like This is u.s.5 if that's desirable.
Just use this query:
SELECT name FROM tbl_name WHERE name REGEXP ""[[:<:]]u\\.s\\.([[:blank:]]|$)"
No need to use end-of-word [[:>:]] on RHS since you already have a dot after s.
In the mysql regexp manual is a table of special chars and howto escape them.
Doing your query like
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u[.]s[.][[:>:]]"
or
SELECT name FROM tbl_name WHERE name REGEXP "[[:<:]]u[[.period.]]s[[.period.]][[:>:]]"
will work