Question

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.

Était-ce utile?

La 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
}

Autres conseils

var regex = new Regex(@"T\.m\((?<MyGroup>.*)\)");
var match =regex.Match(subject);
if(match.Success)
{
    var found =  match.Groups["MyGroup"].Value;
}

If you use a regex and want all the parts seperate, this might work

 #  @"(?s)T\.m\(((['""])((?:(?!\2).)*)\2)\)"

 (?s)                          # dot all modifier
 T \. m                        # 'T.m'
 \(                            # Open parenth
 (                             # (1 start), Quoted part
      ( ['"] )                      # (2), Open Quote
      (                             # (3 start), Body of quoted part
           (?:                           # grouping
                (?! \2 )                      # lookahead, not a quote
                .                             # any char
           )*                            # end grouping, do 0 or more times
      )                             # (3 end)
      \2                            # Close quote (group (2) backreference)
 )                             # (1 end), Quoted part
 \)                            # Close parenth

Just take what you need from the groups

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top