1

I have a c# code, a regex expression, and HTML source file. It works fine in RegexBuddy and http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx, but not in Visual Studio. Please help and explain me what is wrong.

What I expect is:

Found 3 matches:

/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username</a></td> <td bgcolor="#F6F6F6"><a href="../../msg/?id=49244417" onclick="return openUrl(this.href);">Message1 example text</a></td> <td bgcolor="#F6F6F6">16.10.11 23:20</td> has 5 groups:
    1. 4387740
    2. Username
    3. 49244417
    4. Message1 example text
    5. 16.10.11 23:20
/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username2</a></td> <td><a href="../../msg/?id=49223017" onclick="return openUrl(this.href);">Message2 example text</a></td> <td>16.10.11 14:42</td> has 5 groups:
    1. 4387740
    2. Username2
    3.49223017
    4. Message2 example text
    5. 16.10.11 14:42
/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username3</a></td> <td bgcolor="#F6F6F6"><a href="../../msg/?id=49222720" onclick="return openUrl(this.href);">Message3 example text</a></td> <td bgcolor="#F6F6F6">16.10.11 14:34</td> has 5 groups:
    1. 4387740
    2. Username3
    3. 49222720
    4. Message3 example text
    5. 16.10.11 14:34

Regex

    @"/setcard/\?set=([0-9]*).*;"" >(.*)</a></td>$\s.*/msg/\?id=([0-9]*).*ref\);"">(.*)</a></td>$\s\s?.*>(.*)</td>$"

C# Code

using (StreamReader rdr = File.OpenText("file.html"))
        { s = rdr.ReadToEnd(); }

        Regex listMsgs = new Regex(@"/setcard/\?set=([0-9]*).*;"".>(.*)</a></td>$
.*/msg/\?id=([0-9]*).*ref\);"">(.*)</a></td>$

?.*>(.*)</td>$", RegexOptions.Multiline);
        Match m = listMsgs.Match(s);
        while (m.Success)
        {}

HTML Source

        <td bgcolor="#F6F6F6" class="c1"><IMG BORDER="0" SRC="transparent.gif" width="15px" height="15px" /></td>
        <td bgcolor="#F6F6F6" style="width:108px"><a href="../../auswertung/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username</a></td>
        <td bgcolor="#F6F6F6"><a href="../../msg/?id=49244417" onclick="return openUrl(this.href);">Message1 example text</a></td>
        <td bgcolor="#F6F6F6">16.10.11 23:20</td>
        <td bgcolor="#F6F6F6">
                    </td>
        <td bgcolor="#F6F6F6" align="center">

        <img src="message_art1.gif" width="14" height="10" border="0" />            </td>
        <td bgcolor="#F6F6F6"><input type="checkbox" name="messages[]" id="id_msg_1" value="49244417"></td>
        </tr>
                                <tr height="20">
        <td class="c1"><IMG BORDER="0" SRC="transparent.gif" width="15px" height="15px" /></td>
        <td style="width:108px"><a href="../../auswertung/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username2</a></td>
        <td><a href="../../msg/?id=49223017" onclick="return openUrl(this.href);">Message2 example text</a></td>

        <td>16.10.11 14:42</td>
        <td>
                    </td>
        <td align="center">
        2           </td>
        <td><input type="checkbox" name="messages[]" id="id_msg_2" value="49223017"></td>
        </tr>
                                <tr height="20">

        <td bgcolor="#F6F6F6" class="c1"><IMG BORDER="0" SRC="transparent.gif" width="15px" height="15px" /></td>
        <td bgcolor="#F6F6F6" style="width:108px"><a href="../../auswertung/setcard/?set=4387740&t=1&secure=xJHC9dYymGSnImebS4qLPw%3D%3D" onclick="return openUrl(this.href);" >Username3</a></td>
        <td bgcolor="#F6F6F6"><a href="../../msg/?id=49222720" onclick="return openUrl(this.href);">Message3 example text</a></td>
        <td bgcolor="#F6F6F6">16.10.11 14:34</td>
        <td bgcolor="#F6F6F6">
                    </td>
        <td bgcolor="#F6F6F6" align="center">

        2           </td>
        <td bgcolor="#F6F6F6"><input type="checkbox" name="messages[]" id="id_msg_3" value="49222720"></td>
        </tr>
                                <tr height="20">
2
  • how about describing your results vs what you were expecting. Commented Oct 19, 2011 at 0:09
  • Hi Boo, I edited my question... Commented Oct 19, 2011 at 21:59

2 Answers 2

1

This regex yields the expected result for me:

@"/setcard/\?set=([0-9]*).*?;""\s*>(.*?)</a></td>\s*.*?/msg/\?id=([0-9]*).*?ref\);"">(.*?)</a></td>\s*.*>(.*?)</td>"

It looks like you're using the $ metacharacter to match newlines, which is incorrect. That's a zero-width assertion: it matches the position just before a newline, without consuming the newline character(s). That means the .* following the $ has to consume it, but of course the dot doesn't match newlines.

There's really no point in using the anchor ($) in this case; you have to consume the newlines anyway, so just match them the way you match any other characters. If the newlines were required I would suggest using [\r\n]+, which will match one or more of any kind of newlines, whether they're \r\n (DOS/Windows style), \r (pre-OSX Mac), or \n (everything else). But in this case I don't think you need to be that specific; \s* (zero or more of any whitespace characters) seems to work fine. You also don't need the Multiline option any more.

Sign up to request clarification or add additional context in comments.

Comments

0

Anywhere you're expecting any sort of newline character, you need to use a '\s' in your regex. Check out a similar question and answer here for more details.

1 Comment

Hi jm2, thans for your help. I tried to use /setcard/\?set=([0-9]*).*;" >(.*)</a></td>$\s.*/msg/\?id=([09]*).*ref);">(.*)</a></td>$\s\s?.*>(.*)</td>$ and it still works online and in RegexBuddy, but not in c#... any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.