• 0

[C#] Regular expressions


Question

I have this html code:

$FOR_EACH_ITEM$<<

<TR>

<TD CLASS=$STYLE$ WIDTH=100>
</TD>

<TD>
</TD>

</TR>

>>

And I'm trying to get all of the text which appears between $FOR_EACH_ITEM$<< and >> into one string using regex.replace.

However, I have no idea how it should be done :no: Can you tell me how to do it ?

Link to comment
https://www.neowin.net/forum/topic/298833-c-regular-expressions/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

This worked for me.

string ResultString = null;
try {
	ResultString = Regex.Match(SubjectString, "&lt;([A-Z][A-Z0-9]*)[^&gt;]*&gt;(.*?)&lt;/\\1&gt;",
  RegexOptions.Singleline | RegexOptions.IgnoreCase).Value;
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}

  • 0

:laugh: :) :D Thanks a lot weenur. You are really generous :)

It works when the text is just what I showed in the 1st post, but actually I need it to work with this string:

&lt;HTML&gt;
&lt;HEAD&gt;
&lt;/HEAD&gt;
&lt;BODY topmargin="0" leftmargin="0"&gt;
&lt;TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0&gt;
		&lt;TR&gt;
			&lt;TD style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr=#FFFFFF, startColorstr=#7DA3F9, gradientType='1');" WIDTH=100% HEIGHT=31  VALIGN="Center"&gt;
				&lt;P align="center" STYLE="font-style: normal"&gt;&lt;FONT COLOR="#0E60F8"&gt;&lt;FONT SIZE=5 STYLE="font-size: 20pt"&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
			&lt;/TD&gt;
		&lt;/TR&gt;

				$FOR_EACH_ITEM$&lt;&lt;

		&lt;TR&gt;
			&lt;TD style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr=#FFFFFF, startColorstr=#EAF0FE, gradientType='1');" WIDTH=100% VALIGN="Center"&gt;
				&lt;UL&gt;&lt;li style="list-style-type: disc; color: #0E60F8"&gt;&lt;P&gt;
					&lt;FONT COLOR="#7EA3F9" STYLE="font-size: 12pt"&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;/P&gt;
				&lt;/UL&gt;
			&lt;/TD&gt;
		&lt;/TR&gt;
		&lt;TR&gt;	

	&gt;&gt;

			&lt;TD WIDTH=100% HEIGHT=31 VALIGN=Center BGCOLOR="#B1C8FB"&gt;
				&lt;ul&gt;&lt;li style="list-style-type: disc; color: white;"&gt;&lt;FONT COLOR="#ffffff"&gt;&lt;FONT STYLE="font-size: 10pt"&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;br&gt;
                         &lt;li style="list-style-type: disc; color: white;"&gt;&lt;FONT COLOR="#ffffff"&gt;&lt;FONT STYLE="font-size: 10pt"&gt;&lt;B&gt;&lt;/B&gt;&lt;/FONT&gt;&lt;/FONT&gt;
				&lt;/ul&gt;
			&lt;/TD&gt;
		&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;

Can you please tell me how to get it ?

I really appreciate your help :laugh: Thank you very much ! :)

  • 0

OK. My advice. Don't use regular expressions. You know what your token is, so use IndexOf and LastIndexOf to find everything inbetween.

  	string s1;
  	const string token1 = "$FOR_EACH_ITEM$&lt;&lt;";
  	const string token2 = "&gt;&gt;";
  	s1 = textBox1.Text;
  	s1 = s1.Substring( s1.IndexOf( token1 ) + token1.Length ).Trim();
  	s1 = s1.Substring( 0, s1.IndexOf( token2 ) ).Trim();
  	textBox2.Text = s1;

  • 0

why not just do this

Dim s = your html string
Dim startstring As String = "$FOR_EACH_ITEM$&lt;&lt;"
Dim endstring As String = "&gt;&gt;"
Dim startindex As Integer = s.IndexOf(startstring) + startstring.Length
Dim endindex As Integer = s.IndexOf(endstring)
Dim bs As String = s.Substring(startindex, endindex - startindex)

hey weenur :p my first reply in some time.

Edit: HAHAHAHA he acutally beat me to it.

  • 0
  Bender said:
why not just do this

Dim s = your html string
Dim startstring As String = "$FOR_EACH_ITEM$&lt;&lt;"
Dim endstring As String = "&gt;&gt;"
Dim startindex As Integer = s.IndexOf(startstring) + startstring.Length
Dim endindex As Integer = s.IndexOf(endstring)
Dim bs As String = s.Substring(startindex, endindex - startindex)

hey weenur :p my first reply in some time.

Edit: HAHAHAHA he acutally beat me to it.

585657616[/snapback]

Edit: you could do it with regular expressions but youd have to remove all the \r\n

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • TDP of this CPU is 60 watts higher than Ryzen 7600. At s usage rate of four hours per day, at a cost of twelve cents per KWh, the Intel cost $10.51 more per year to use. I don't see a real advantage to Intel here.
    • Lmao. Cries about not playing those games not installed and yet don't ever want to touch them.
    • If I want to merge folder trees that have a similar structure, Beyond Compare is always my first choice. It's not free but it's awesome. If I want to just scan a whole drive/folder and find duplicates that are taking up space, I like Czkawka.
    • Claude Code gets throttled as Anthropic rolls out fresh usage caps by David Uzondu Claude Code, the AI-in-terminal utility developed by Anthropic and launched back in February, is getting updated usage limits following weeks of user complaints about being abruptly cut off. Many developers on the "$200/month Max plan" found their access blocked after just a few requests, with no explanation from the company. In a recent thread posted to X, the AI lab explained that it has seen "unprecedented demand since launch," pointing to some of its heaviest users who were running the tool continuously in the background 24/7, with one person reportedly consuming tens of thousands of dollars in model usage on a single $200 subscription. Anthropic also claimed that some users were violating its usage policy by sharing and reselling accounts, which impacts system capacity for everyone. These factors all led the company to announce new weekly limits that will be added on top of the existing five-hour caps, effective August 28. Max plan subscribers will have the option to buy additional usage at standard API rates if they hit their cap. Here's what the new weekly limits look like: Pro Plan ($20/month): An estimated 40 to 80 hours of usage with the Sonnet 4 model. Max Plan ($100/month): An estimated 140 to 280 hours with Sonnet 4 and 15 to 35 hours with the top-tier Opus 4 model. Max Plan ($200/month): An estimated 240 to 480 hours with Sonnet 4 and 24 to 40 hours with Opus 4. Per TechCrunch, the company provided these hour-based estimates, noting that the actual numbers may vary based on the size of a project's codebase. What's interesting is how this new structure compares to the old marketing. Anthropic previously advertised its $200 Max plan as offering 20 times more usage than the Pro plan. Based on these new hourly estimates, that multiple is now closer to six. It is possible the 20x figure still applies when measured in tokens or raw compute, but, according to TechCrunch, the company has not clarified that point.
    • I don't give a rat's f### what Trumpette, the Putin puppet likes!
  • Recent Achievements

    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      645
    2. 2
      ATLien_0
      260
    3. 3
      Xenon
      165
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      107
  • Tell a friend

    Love Neowin? Tell a friend!