正则表达式找到GetText字符串“<%$ GetText:”

时间:2011-04-05 17:02:50

标签: c# regex xgettext

我需要从此代码中获取“Home”或“Home”文本:

<%@ 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**"%>'/>

找到这个有正则表达式吗?

我可以逐行搜索“&lt;%$ GetText:”或“&lt;%$ GetText:”当然可能有更聪明的人:)

THX

3 个答案:

答案 0 :(得分:1)

获取结果并通过它们进行交互。这匹配GetText的任何cAsE并将所有内容都包含在单引号或双引号内(使用逐字字符串也可以为您节省大量的转义字符):

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
}

答案 1 :(得分:0)

这个正则表达式将从第1组中的每个示例中获得回家。

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

答案 2 :(得分:0)

正则表达式:

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

如果您使用来自System.Text.RegularExpressions的.NET命名捕获,可以按如下方式修改正则表达式:

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

...您的目标文字位于匹配组“mytext”

相关问题