简体   繁体   中英

How to match specific string pattern using REGEXP (or similar) in MySQL

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 az, AZ and digits 0-9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM