Search code examples
c#regexstringsubstringextract

Extracting sub string with regexp


I have the following kind of text inside my source code files:

html += T.m('current value of the quality level of the security service (as established from the diagnostic phase).');
or
{header: T.m("Service to Improve"), dataIndex : 'searvicesToImprove', hideable: false},
or
Ext.getCmp('MAX_ACTION_PLANS_PAGE').setText(T.m('od') + ' ' + MAX_ACTION_PLANS_PAGE);

I want to extract the substring inside of the brackets, ie. from T.m(X) I want to get the X without the quote brackets or with them, and I would trim them afterwards.

So in other words I would like something like this:

regex( "T.m('X')" | "T.m("X")" );
and then say:
listOfMatches.add(X);

I know this is usually done with regexp, but I'm not that good with regexp, haven't used it much, only for basic samples. Any help is very much appriciated.


Solution

  • try {
        Regex regexObj = new Regex(@"T\.m\([""|'](.+?)[""|']\)");
        Match matchResults = regexObj.Match(subjectString);
        while (matchResults.Success) {
            var neededvalue = matchResults.Groups[1].Value;
        } 
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }