467,145 Members | 956 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

Parsing text for hyperlinks

Say I have

string foo = "Some text http://www.google.com more text
fr*********@somewhere.com 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] = "fr*********@somewhere.com", 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]')

Nov 17 '05 #1
  • viewed: 2252
Share:
2 Replies
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" wrote:
Say I have

string foo = "Some text http://www.google.com more text
fr*********@somewhere.com 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] = "fr*********@somewhere.com", 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]')

Nov 17 '05 #2
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 ",
"fr*********@somewhere.com",
" 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 Ballard wrote:
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" wrote:
Say I have

string foo = "Some text http://www.google.com more text
fr*********@somewhere.com 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] = "fr*********@somewhere.com", 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]')


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Frostillicus | last post: by
19 posts views Thread by Joe | last post: by
8 posts views Thread by Bradley | last post: by
3 posts views Thread by hapaboy2059@gmail.com | last post: by
13 posts views Thread by Chris Carlen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.