简体   繁体   中英

Finding text within text delimited by new line character

I am trying to find text within text using MySQL. I have a field of values that is somewhat unstructured, but the data entry fortunately is delimited by new lines. I'm trying to see if I can pull the value for "Education", which would be basically a substring that starts after "Education:" and ends with \\n new line character in data below:

'Children:  5
Education:  College
Employment: Homemaker
Marital Status: Married'

I've looked at the MID function, but since the values for education vary, the length isn't standard. I have searched MySQL string functions, and I have not found a solution that will allow me to search between two positions, including one that is defined by a regex character -- the REGEX simply provides a match, not a position.

SELECT id,MID(value,POSITION('Education:' IN value),30) 
FROM client_data 

the code performed as expected, but due to fixed length rather than position of \\n new line character, results either truncated or included extra characters from subsequent text.

I'm guessing there is a way to do this that I'm just not finding.

You can use REGEXP_SUBSTR to get actual the string that matched the regular expression:

REGEXP_SUBSTR(value, '^Education:.*', 1, 1, 'm')

This gets you the Education line . Then you just need to extract the part after the : from that string:

REGEXP_REPLACE(
  REGEXP_SUBSTR(value, '^Education:.*', 1, 1, 'm'),
  '^Education:', '')

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