Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract substring from mysql column using regex

My regex is

(pnr|(P|p)[ _.:,!"'-/$](N|n)[ _.:,!"'-/$](R|r))+[ _.:,!"'-/$]+[0-9]{3}[ _.:,!"'-/$]+[0-9]{7}

It is extracting pnr number from column .

sample text :

94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding pnr:986-097832

94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: pnr:986-097832 text/plain; charset=UTF-8\r\nContent-Transfer-Encoding 

pnr:986-097832 94eb2c0cb17ef354bb052c57f40c\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding

I have to create a mysql query which will extract only the pnr number

like image 232
Sunil Harak Avatar asked Feb 28 '17 05:02

Sunil Harak


People also ask

How do I find substrings in regex?

We can use the “i” in the regex to search a substring irrespective of the case sensitivity.

Can MySQL use regex?

MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. It provide a powerful and flexible pattern match that can help us implement power search utilities for our database systems. REGEXP is the operator used when performing regular expression pattern matches.

Is Regexp_like faster than like?

Better Use of LIKE Query instead of REGEXP if you are not sure about value. Also LIKE is much faster than REGEXP.


1 Answers

SELECT REGEXP_SUBSTR(column, 'pnr:[0-9\-]{10}')
FROM table
like image 188
benito Avatar answered Oct 15 '22 13:10

benito