By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Regex to grab keywords from HTML header

P: n/a
I'm trying to figure out how to extract the keywords from an HTML
document.
The input string would typically look like:
<meta name='keywords' content='word1, more stuff, etc'>

Either single quotes or double quotes can be used and there can be any
number of spaces or returns between any element. Keywords can contain
special characters except for a comma or a closed bracket. For
example, the HTML might be:

<
meta name =
'
keywords'
content=
"word1 ,
more
stuff
,
etc"


The coolest thing would be to have a routine actually return one
keyword at a time (the keywords are separated by commas) However, I'd
be happy just to have the routine return only the keywords w/o all the
rest of the surrounding HTML.

Here's what I've tried so far for a Regex string.

"[<][\s\n\r\t]*meta[\s\n\r\t]name[\s\n\r\t]*='[\s\n\r\t]*'[\s\n\r\t]*keywords[\s\n\r\t]content[\s\n\r\t]*=[\s\n\r\t]*'[^>]'[\s\n\r\t]*>"

It's not working very well :) (this regex stuff is complicated!)

Can anybody help a regex newbie?

Nov 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You might want to try Expresso
http://www.ultrapico.com/
or
Regulator (look at google to find out the address)
HIH

<Di**************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g47g2000cwa.googlegr oups.com...
I'm trying to figure out how to extract the keywords from an HTML
document.
The input string would typically look like:
<meta name='keywords' content='word1, more stuff, etc'>

Either single quotes or double quotes can be used and there can be any
number of spaces or returns between any element. Keywords can contain
special characters except for a comma or a closed bracket. For
example, the HTML might be:

<
meta name =
'
keywords'
content=
"word1 ,
more
stuff
,
etc"
>


The coolest thing would be to have a routine actually return one
keyword at a time (the keywords are separated by commas) However, I'd
be happy just to have the routine return only the keywords w/o all the
rest of the surrounding HTML.

Here's what I've tried so far for a Regex string.

"[<][\s\n\r\t]*meta[\s\n\r\t]name[\s\n\r\t]*='[\s\n\r\t]*'[\s\n\r\t]*keywords[\s\n\r\t]content[\s\n\r\t]*=[\s\n\r\t]*'[^>]'[\s\n\r\t]*>"

It's not working very well :) (this regex stuff is complicated!)

Can anybody help a regex newbie?

Nov 23 '05 #2

P: n/a
Try one of the many RegEx sites:

How To Use Regular Expressions in Microsoft Visual Basic 6.0
http://support.microsoft.com/default...b;en-us;818802

RegEx Tutorial for VB:
http://juicystudio.com/tutorial/vb/regexp.asp

RegEx Library:
http://www.regexlib.com/

RegEx Module for VB:
http://www.aivosto.com/regexpr.html

--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
Veign's Blog
http://www.veign.com/blog
--
<Di**************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I'm trying to figure out how to extract the keywords from an HTML
document.
The input string would typically look like:
<meta name='keywords' content='word1, more stuff, etc'>

Either single quotes or double quotes can be used and there can be any
number of spaces or returns between any element. Keywords can contain
special characters except for a comma or a closed bracket. For
example, the HTML might be:

<
meta name =
'
keywords'
content=
"word1 ,
more
stuff
,
etc"
>


The coolest thing would be to have a routine actually return one
keyword at a time (the keywords are separated by commas) However, I'd
be happy just to have the routine return only the keywords w/o all the
rest of the surrounding HTML.

Here's what I've tried so far for a Regex string.

"[<][\s\n\r\t]*meta[\s\n\r\t]name[\s\n\r\t]*='[\s\n\r\t]*'[\s\n\r\t]*keywords[\s\n\r\t]content[\s\n\r\t]*=[\s\n\r\t]*'[^>]'[\s\n\r\t]*>"

It's not working very well :) (this regex stuff is complicated!)

Can anybody help a regex newbie?

Nov 23 '05 #3

P: n/a
Woohoo! Great reference Boni!

Here's the regex string that returns the keywords:
<\s*meta\s*name\s*=\s*"\s*keywords\s*"\s*content\s *=\s*"\s*([^"]+)"\s*>

This makes a lot more sense now...

Is there a way to further parse the keywords inside the ([^"]+)
adding to the string above?

Keywords are listed as
at least one keyword (ending in either quote or comma)
if it ends with a quote, then throw away the quote and we're done.
if it ends in comma then look for repeating groups of [,next keyword]
and throw away the comma each time

I've looked at a number of tutorials online and this part is more
complicated

Note: Veign - the "jucystudio" reference 404'd out :(

Nov 23 '05 #4

P: n/a
<Di**************@gmail.com> schrieb:
I'm trying to figure out how to extract the keywords from an HTML
document.


I'd consider the alternatives to using regular expressions for this purpose:

<URL:http://groups.google.de/group/microsoft.public.dotnet.languages.csharp/msg/d3a373d9d9f8367b>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #5

P: n/a
Di**************@gmail.com wrote:
I'm trying to figure out how to extract the keywords from an HTML
document.
The input string would typically look like:
<meta name='keywords' content='word1, more stuff, etc'>


You have posted this to both a dotnet group and a VB Classic group - the two
are different languages. You need to specify which language you are using,
because...
--
<response type="generic" language="VB.Net">
This newsgroup (.vb.syntax) is for users of Visual Basic version 6.0
and earlier and not the misleadingly named VB.Net
or VB 200x. Solutions, and often even the questions,
for one platform will be meaningless in the other.
When VB.Net was released Microsoft created new newsgroups
devoted to the new platform so that neither group of
developers need wade through the clutter of unrelated
topics. Look for newsgroups with the words "dotnet" or
"vsnet" in their name. For the msnews.microsoft.com news
server try these:

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb

</response>
--
Regards,

Michael Cole
Nov 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.