From Mysql 8, you can use the function REGEXP_SUBSTR
, which returns only the passage corresponding to the regular expression.
However, the regex ', [A-Z]{2}'
will also return the comma and the space. To return only the letters, just use SUBSTR
:
SELECT SUBSTR(REGEXP_SUBSTR(artigo, ', [A-Z]{2}', 1, 1, 'c'), 2)
from artigos WHERE artigo REGEXP ', [A-Z]{2}';
With that, only the letters BA
are returned by query.
The parameters 1, 1, 'c'
are respectively the position where the search starts (1 is the beginning of the string), which occurrence will be returned (1, because we want the first occurrence of regex in the string) and 'c'
makes the search be case sensitive *.
See working on DB Fiddle.
* At first a regex [A-Z]
should already pick only capital letters, but testing on DB Fiddle he was doing the search case insensitive and ended up picking up the letters co
of the stretch , consectetur
. Passing the option 'c'
the search became case sensitive and the BA
was found correctly. Probably this happens because the default mysql is to use the charset rules and collation search parameters, and DB Fiddle must use one whose default is case insensitive. To learn more, see here and here.
For versions prior to Mysql 8, the function REGEXP_SUBSTR
is not available. In this case, maybe it is possible to simulate the result (or get close to it) with substrings, using something like this. Or simply take the entire string and extract the desired chunk using a programming language - as these usually have better regex support than Mysql.
Missed the
WHERE
in the query:SELECT artigo from artigos WHERE artigo REGEXP ', [A-Z]{2}'
. Or was typing error when posting the question?– hkotsubo
It was a typo. The query returns the entire article. I wanted it to return, in the example only 'BA'.
– zwitterion