2

Need a Regex solution to select only a pattern of string from a column.

Data looks like this:

Column1
Data Type = String 

Data = 
"130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12"

Expected Result

66, 3, 18, 2, 14, 8, 31, 12

Tried REgex - "\-(...*?)\W" but it doesn't work.

2
  • What does SELECT VERSION(); return? Commented Jul 16, 2019 at 21:42
  • 1
    SELECT VERSION() = 8.0.16 Commented Jul 16, 2019 at 21:50

2 Answers 2

2

You can try this

SELECT REPLACE(REGEXP_REPLACE('130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12',
                                     '(([,0-9]+) - )', ''),'||',',');

and result

66,3,18,2,14,8,31,12

Referance

Sign up to request clarification or add additional context in comments.

4 Comments

This works Ferhat, what if i need the values before - like 130, 109,118,102,109,118,109,116,149,141,130,102
I am not good the regular expresision but when i need regex I use it this site regex101.com and try it
Just use this regex "(- [0-9]+)" finding others
Got it , Thanks Ferhat
0

MySQL REGEXP returns a boolean, not a string.

excerpt from MySQL Reference Manual:

Returns 1 if the string expr matches the regular expression specified by the pattern pat, 0 otherwise. If expr or pat is NULL, the return value is NULL.

Maybe we want to use MySQL REGEXP_REPLACE function ?

Reference:

https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp

https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace

1 Comment

Thanks Spencer, Tried but it is just removing the '-' sign select REGEXP_REPLACE('130 - 66||109,118 - 3||102 - 18||109,118 - 2||109,116,149 - 14||141 - 8||130 - 31||102 - 12', '-',''); Output - 130 66||109,118 3||102 18||109,118 2||109,116,149 14||141 8||130 31||102 12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.