Parsing text for hyperlinks

S

Sam Collett

Say I have

string foo = "Some text http://www.google.com more text
(e-mail address removed) more";

How could I parse it to find the links and store the results in a
string array, i.e. to get

string[] bar = ParseText(foo);
// bar[0] = "Some text ", bar[1] = "http://www.google.com", bar[3] = "
more text ", bar[4] = "(e-mail address removed)", bar[5] = " more"


The string variable 'foo' will have much more text than this. I don't
expect many hyperlinks, and in some cases none (so there will be a
'bar[0]', but not 'bar[1]')
 
G

Guest

Sam,

Take a look at the docs for System.Text.RegularExpressions.Regex. iirc some
of the examples there show how to pick out URLs and email addresses.

If you havent looked at RegEx before, it is a very powerful class, and quite
easy to use once you get the hang of it.
 
S

Sam Collett

I've used a regular expression to replace http://example.com with <a
href="http://example.com">http://example.com</a>

But that is not what I want to do. I want to create a string array
like:

string[] bar = {"Some text ",
"http://www.google.com",
" more text ",
"(e-mail address removed)",
" more"}

It is not hard coded - i.e. text from a database table is stored in the
variable 'foo', which is then parsed to create a string array
resembling 'bar'

Then I use the iTextSharp PDF generator

Phrase p = new Phrase();

Chunk ch;
foreach(string baz in bar)
{

if(IsHyperLink(baz))
{
ch = new Chunk(baz,linkFont);
ch.SetAnchor(baz);
}
else if(IsEmail(baz))
{
ch = new Chunk(baz,linkFont);
ch.SetAnchor("mailto:" + baz);
}
else
{
ch = new Chunk(baz,normalFont);
}
p.Add(ch);
}

Chris said:
Sam,

Take a look at the docs for System.Text.RegularExpressions.Regex. iirc some
of the examples there show how to pick out URLs and email addresses.

If you havent looked at RegEx before, it is a very powerful class, and quite
easy to use once you get the hang of it.
--
Chris Ballard
MCSD.NET


Sam Collett said:
Say I have

string foo = "Some text http://www.google.com more text
(e-mail address removed) more";

How could I parse it to find the links and store the results in a
string array, i.e. to get

string[] bar = ParseText(foo);
// bar[0] = "Some text ", bar[1] = "http://www.google.com", bar[3] = "
more text ", bar[4] = "(e-mail address removed)", bar[5] = " more"


The string variable 'foo' will have much more text than this. I don't
expect many hyperlinks, and in some cases none (so there will be a
'bar[0]', but not 'bar[1]')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top