1

我需要从这段代码中获取文本“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**"%>'/>

有没有找到这个的正则表达式?

当然,我可以逐行搜索“<%$ GetText:”或“<%$GetText:”,但也许有人更聪明:)

谢谢

4

3 回答 3

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
}
于 2011-04-08T01:36:48.683 回答
0

正则表达式:

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

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

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

...并且您的目标文本位于匹配组“mytext”中

于 2011-04-05T17:12:45.207 回答
0

此正则表达式将从第 1 组中的每个示例中获取 Home。

\*\*(\w+)\*\*
于 2011-04-05T17:08:13.817 回答