Search code examples
regexmatlabstrsplit

Split a string into two parts in matlab


Input strings are :

InputStr1 = 'this-is-a-boy-5';
InputStr2 = 'this23-is-a-boy-10';
InputStr3 = 'this-41';

Output should be :

Output1 = ['this-is-a-boy'] [5]
Output2 = ['this23-is-a-boy'] [10]
Output3 = ['this'] [41]

I want to split these strings into two parts such that I can separate the first string and last number from it. I have tried strsplit() but It didn't help.


Solution

  • Try splitting on the - which appears before number from end of string.

    Regex: -(?=\d+$)

    Explanation:

    • (?=\d+$) looks ahead if the number is at end of string. And matches the - before it. You can split on this.

    Regex101 Demo