1

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

Joe
  • 56,979
  • 9
  • 128
  • 135
user324365
  • 89
  • 7

3 Answers3

1

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
}
Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
  • It finds everything and returns what is between : and %. but only if it's in between quotes. This is also correct $GetText:XXXX%> = XXXXX My first question wasn't that clear. But can't edit it. Sorry. – user324365 Apr 09 '11 at 10:19
  • In the values the text "GetText" is also returned. I would like only what is between : and % – user324365 Apr 09 '11 at 10:24
  • if you want other parts to be returned as groups you can set more parenthesees (capture groups) – Jim Wolff Apr 09 '11 at 11:43
  • Your answer is the closest. Could you give an example of such a group? – user324365 Apr 18 '11 at 09:45
  • @user324365 If you look at the regex pattern, and notice what the parentheses encapsulate, this is what gets captured in the capture group, they are numbered as the order they appear in the regex. you can also use a named capture group if you want to do it by name instead of the number. numbered:(content) named:(?content) if this doesnt clarify please tell me which parts that need futher explanation – Jim Wolff Apr 22 '11 at 14:01
0

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

\*\*(\w+)\*\*
Joe
  • 56,979
  • 9
  • 128
  • 135
  • I ran that against your your example and I got Home every time. – Joe Apr 06 '11 at 12:40
  • probably because the test data you supplied here is not the same as you are testing against. Joe is correct. but if you have more words, spaces, specials characters `\*\*(\w+)\*\*` wont match. it only matches a single word inside a line like this: `**singleword**` – Jim Wolff Apr 08 '11 at 01:39
  • Yes FRoZeN is correct. `\*\*(.+)\*\*` may be what you are looking for if you want anything in between *'s. If that does not help you you really need to post a more realistic set of examples you are testing against. – Joe Apr 08 '11 at 12:41
  • I understand why it doesn't work. Im not looking for * * home * * (without the spaces) but for the text in between the single quotes. Setting text to bold in the stackoverflow editor add the asterixs i.e. 'abc -> * * abc * * ' (without the spaces) – user324365 Apr 09 '11 at 09:34
0

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"

Ken Gregory
  • 7,180
  • 1
  • 39
  • 43
  • I forgot to tell that "GetText" can be "gettext" or "GetTEXT" – user324365 Apr 05 '11 at 22:09
  • 1
    You can run the regular expression so that it ignores case. When you say "\s isn't allowed" are you asking about the function of \s* or reporting an error with it? – Ken Gregory Apr 06 '11 at 21:15
  • this is my code: Match m = Regex.Match(line, "GetText:\s*[\'\"]([^\'\"]+)[\'\"] "); VS2010 says: unrecognized escape sequence – user324365 Apr 07 '11 at 00:13
  • Sorry. Forgot to escape the : after GetText. Put a \ before it. – Ken Gregory Apr 07 '11 at 03:05
  • `:` does not need to be escaped. try using a verbatim string be putting a @ in front of your regex string, this way you can remove most escapes `"GetText:\s*['"]([^'"]+)['"] ");` `\s` is regex and still needs to be there :) – Jim Wolff Apr 08 '11 at 01:43
  • With the second \ it works as asked. But the results are without the 'or ". Is this possible? '<%$GetText:-IwantWhatIsBetweenThe:And%-%>'/> '<%$GetText:**Home**%>'/> This one is also a correct find – user324365 Apr 09 '11 at 09:54