Jump to content

Need some help with Regex

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Edvinas

Edvinas

    Newbie

  • Members
  • PipPip
  • 10 posts
I'm using Regex to find some things in one websites source and then add all the things found in textbox in certain format (layout).

I know how to do regex etc. and everything works fine for me, but im stuck now when one of the things i need to find are not in the same line, they are structured one per line.

example of how it is in in the website source:

 
<li class="test"><div style="background-color:blue">Hello</div></li>  
<li class="test"><div style="background-color:blue">Hello1</div></li>  
<li class="test"><div style="background-color:blue">Hello2</div></li> 


basically i need only those "Hello", "Hello1" etc., so i have this regex which finds this pattern:
"<li class='test'><div style='background-color:blue'>(.+?)</div></li>"

And it works fine, it finds those "Hello" etc. which i want to find, but the problem is that i need ALL of them to be collected and put in one line seperated by commas in certain structure

To put the results in a multiline textbox, i use something like:

textBox1.Text = Environment.NewLine + textBox1.Text + Environment.NewLine +  "[start]" + Environment.NewLine + "string is: " + m.Group[1].ToString() + Environment.NewLine + "[end];

which prints out in layout like this:

 
[start]
string is Hello
[end]

But the problem is, that I want it to put that "Hello", "Hello1" and "Hello2" in same line seperated by commas like this:

 
[start]
string is Hello, Hello1, Hello2
[end]

What ever I try I just can't get it to work, the way it does it for me is like this...:

 
[start]
string is Hello
[end]
 
[start]
string is Hello1
[end]
 
[start]
string is Hello2
[end]

I know it's easy to do in php using "foreach" but it doesnt work for me on C# :S

Can someone pelase help me out here?

thanks.

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
try this might work:

textBox1.Text +=   Environment.NewLine +  "[start]" + Environment.NewLine + "string is: " + m.Group[1].ToString() + ",[end]";


#3
Edvinas

Edvinas

    Newbie

  • Members
  • PipPip
  • 10 posts
Nah doesn't work. It's the same as the way I was doing it. :(

Here's an example program that I quickly done to make it easier for you to understand what I meant with all that stuff above:

[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Collections.Generic;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.ComponentModel;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Data;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Linq;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Text;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Text.RegularExpressions;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WindowsFormsApplication19
{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]partial[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Form1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Form
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Form1()
{
InitializeComponent();
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)
{

[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] source = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<li class='colorbutton'><div style='background-color:DarkSlateBlue'>Hello</div></li>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine + 
[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<li class='colorbutton'><div style='background-color:DarkSlateBlue'>Hello1</div></li>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+ [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine +
[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<li class='colorbutton'><div style='background-color:DarkSlateBlue'>Hello2</div></li>"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Regex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] r1 = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Regex[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"<li class='colorbutton'><div style='background-color:DarkSlateBlue'>(.*?)</div></li>"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]RegexOptions[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Singleline);

[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Match[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] m1 = r1.Match(source); m1.Success; m1 = m1.NextMatch())
{


textBox1.Text += [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"[Start]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine +
[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"The string is: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + m1.Groups[1] + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine +
[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"[End]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Environment[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].NewLine;
}
}

}
}
[/SIZE]

And this outputs:

[Start]
The string is: Hello
[End]
[Start]
The string is: Hello1
[End]
[Start]
The string is: Hello2
[End]

But what I want it to output is:

[Start]
The string is: Hello, Hello1, Hello2
[End]

Thanks

#4
Edvinas

Edvinas

    Newbie

  • Members
  • PipPip
  • 10 posts
bump, really need this :(