Search code examples
c#regexxgettext

Regex to find GetText strings "<%$ GetText:"


I need to grab the text "Home" or 'Home' from this code:

<%@ Page Title="<%$ GetText: '**Home**' %>" Language="C#" ..%>
<asp:Button Text="<%$ GetText: '**Home**' %>"/>
<asp:Button Text='<%$ GetText: "**Home**" %>'/>
<asp:Button Text="<%$GetText:'**Home**'%>"/>
<asp:Button Text='<%$GetText:"**Home**"%>'/>

Is there a regex for finding this?

I can do a search line by line for "<%$ GetText:" or "<%$GetText:" of course but maybe there is someone smarter :)

Thx


Solution

  • Get results and interate through them. This matches for any cAsE of GetText and takes everything inside the single or double quotes (using a verbatim string also saves you a lot of escape chars):

    try {
        Regex regexCard = new Regex(@"([Gg][Ee][Tt]{2}[Ee][Xx][Tt]):\s*(['""""]([^'""""]+)['""""])");
        Match matchResults = regexCard.Match(subjectString);
        while (matchResults.Success) {
    
            //if i is 0 it returns the entire match
            //if i is 1 it returns the first capture group in this case "gettext" without the quotes.
            //if i is 2 it returns the second capture group, everything after <:><0-1 whitespace> including the first <' or "> until it hits a <' or "> (also includes this)
                //if i is 3 it returns the same but without the quotes.
            //you can move parentheses around or make more to create more capture groups to get the text you want.
            if(matchResults.Groups[i].Success)
            {
                string value = matchResults.Groups[i].Value;
            }
            matchResults = matchResults.NextMatch();
        } 
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }