用于查找 GetText 字符串“<%$ GetText:”的正则表达式

发布于 2024-10-30 17:16:10 字数 469 浏览 0 评论 0 原文

我需要从此代码中获取文本“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:”,但也许有人更聪明:)

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生寂 2024-11-06 17:16:10

获取结果并通过它们进行交互。这与 GetText 的任何情况匹配,并获取单引号或双引号内的所有内容(使用逐字字符串还可以节省大量转义字符):

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
}

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
}
涙—继续流 2024-11-06 17:16:10

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

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

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

\*\*(\w+)\*\*
怀中猫帐中妖 2024-11-06 17:16:10

正则表达式:

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

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

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

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

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文