473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
2 2416
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JimJones | last post by:
Hey, I don't really know much about ASP but I need some help modifying the way a field is currently displayed. Currently the ASP code inside my page is simply: <%=rs.Fields("Notes")%> ...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
11
by: Frostillicus | last post by:
I'm having difficulty creating a menu on the left of a web page where each link appears to have a 4px padding around the edge but I'm trying to do this as a pure unordered list. The "current web...
19
by: Joe | last post by:
I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred...
8
by: Bradley | last post by:
I've loaded an RTF file into a RichTextBox. I have included some internal hyperlinks to simplify movement around the file, but the hyperlinks aren't active when loaded into the RichTextBox and...
4
by: Seefor | last post by:
Hi, I want my text hyperlinks to have a dotted border underneath, so I did this which works fine: a, a:link, a:visited, a:hover, a:active { color: #000; text-decoration: none;
3
by: hapaboy2059 | last post by:
Hello, I need help in using sgmlparser to parse a html file and keep track of the number of times each tag is being used. In the end of this program I need to print out the number of times...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.