Search code examples
mysqlsqlregexregex-groupregexp-replace

Regex to find anything after 4th Slash


I need your help in writing regex statement for the string /AB/PS/EUR/ES_Electr_ML_TB

I need to detect the anything written between 4th slash (/) and the first underscore (_), meaning that in the above example, the output should be ES, as it is between 4th slash and then the first underscore.


Solution

  • You could use substring_index() to get the desired result:

    select
      substring_index(substring_index('/AB/PS/EUR/ES_Electr_ML_TB', '/', -1), '_', 1)
    

    It will always look at the following characters after the last slash (/), and then at the characters before the first underscore (_). So in this example, the query output would be ES.