开发者

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

开发者 https://www.devze.com 2023-02-22 19:45 出处:网络
I need to grab the text \"Home\" or \'Home\' from this code: <%@ Page Title=\"<%$ GetText: \'**Home**\' %>\" Language=\"C#\" ..%>

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**"%>'/>
开发者_StackOverflow社区

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


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
}


This regex will get Home from each of those examples inside of group 1.

\*\*(\w+)\*\*


RegEx:

GetText:\s*[\'\"]([^\'\"]+)[\'\"]

If you use .NET's named capture from System.Text.RegularExpressions, the regex can be modified as follows:

GetText:\s*[\'\"](?<mytext>[^\'\"]+)[\'\"]

...and your targeted text is in the match group "mytext"

0

精彩评论

暂无评论...
验证码 换一张
取 消