Utilizing regular expressions in MySQL using SQL

Here, you can examine and experiment with various examples.
Solution 3:
The simplest approach involves using the SQLFiddle demo, as well as the NOT REGEXP operator in MySQL. This operator serves as a negation of the REGEXP operator, which is utilized for pattern matching. The details of this operator include:
– 0 or more spaces
– 1 or more digits
– an optional sequence of a comma or period followed by 1 or more digits
– an optional sequence of “a” followed by 1 or more digits, optionally followed by “or” and 1 or more digits
– 0 or more spaces
Solution 2:
If you prefer to utilize regexp, you can try using either to solely obtain all letters or to include the slash as well.


Solution 1:

The regex pattern in MySQL is placed after the RLIKE, while the input data is positioned before it.

SELECT * FROM `activities` WHERE "/interpretations/q44nAj91aH6nd/" RLIKE `url_regex`;

PS, your regular expression contains a syntax error within the character range. Consider using this alternative instead:

/interpretations/[a-zA-Z0-9]+/

Before testing your regexes, simply provide the input and pattern as direct strings.

SELECT * FROM `activities` WHERE  '/interpretations/q44nAj91aH6nd/' RLIKE  '/interpretations/[a-Z0-9]+/';

The query does not display an “invalid character range” error, as the pattern error does not stop the query, but simply omits the row.

Testing/demo can be accessed at: http://sqlfiddle.com/#!9/82e917/2


Solution 2:



RLIKE

takes into consideration the sequence of the parameters. Give it a try:
“””.

SELECT *
FROM `activities`
WHERE '/interpretations/q44nAj91aH6nd/' RLIKE url_regex;

Change the regular expression to:

INSERT INTO `activities` (`id`, `url`, `method`, `url_regex`)
    VALUES (1, '/interpretations/', 'POST', ''),
           (2, '', 'GET', '/interpretations/[a-zA-Z0-9]+/');

Here is a SQL Fiddle.

Frequently Asked Questions