Search code examples
mysqlregexp-substrrlike

MYSQL REGEXP_SUBSTR: get string or ip out of text


I struggle through a complex query and need help with a REGEXP_SUBSTR command being used in the WHERE CLAUSE: In deed this are two questions.

  1. given three possible records, I want to get the part between the brackets, but only if they match something that looks like an ip. In one case the question is simply between brackets like "[192.168.178.21]"
  2. other case is the text is like "sender=<some192-168-178-12.example.here>"

Case 1:

SELECT REGEXP_SUBSTR('This is a sample of [192.168.178.12] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender
SELECT REGEXP_SUBSTR('This is a sample of [dyn-192-168-178-12.example.com] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender
SELECT REGEXP_SUBSTR('This is a sample of [only.example com] containing relevant data','(?<=[ ]\[)[^\]]#]+') AS sender

The problem above is that escaping does not work the way I've expected. Expected would be:

127.0.0.1
dyn.127.0.0.1.example.com
NULL

Case 2:

If I search in the field using the WHERE CLAUSE I have limited success with

WHERE ( sndtext RLIKE ' sender=\\<.*[0-9]{1,3}(.|-|_)[0-9]{1,3}(.|-|_)[0-9]{1,3}(.|-|_)[0-9]{1,3}.*\\>')

but the match is too broad, it needs to stop at the '>'

How can I achieve both solution in a WHERE CLAUSE?


Solution

  • Might not be perfekt but my solution was (less escaping):

    SELECT 
       REGEXP_SUBSTR(msgtext, '(?<=[[])[^]]+') AS ip,
       REGEXP_SUBSTR(msgtext,'(?<=[ ]sndtext=<)[^>]+') AS txt
    FROM txtmessage m
       WHERE msgtext RLIKE ' sndtext=<.*([0-9]{1,3}[[:punct:]]){3}[0-9]{1,3}[^>]'