Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript Regular Expression Parse HTML file for some key items

Posted on 2008-10-08
7
Medium Priority
?
2,472 Views
Last Modified: 2012-05-05
Good morning experts...  I am in need of some Regular Expression help.
I need to parse the following HTML into 4 variables...

Title = Betweent the <title> tags
KeyWords = content value of keywords metatag
Description = content value of description metatag
PageContent = everything between the <body> tags


<html>
    <head>
        <title>This is my page title</title>
        <meta content="these are my keywords" name="keywords" />
        <meta content="this is the page description" name="description" />
    </head>
    <body>
       This is the HTML document content.
    </body>
</html>

Open in new window

0
Comment
Question by:midcompweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:vsudip
ID: 22669300
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22669315

<%
Set regEx = New RegExp
regEx.Global = True
regEx.IgnoreCase = True
sourcestring = "your source string"
regEx.Pattern = "([^<>]*)</title>[\S\s]*?<meta content=""([^""]*)"" name=""keywords"" />[\S\s]*?<meta content=""([^""]*)"" name=""description"" />[\S\s]*?<body>([\S\s]*?)</body>"
Set Matches = regEx.Execute(sourcestring)
  For z = 0 to Matches.Count-1
    results = results & "Matches(" & z & ") = " & chr(34) & Server.HTMLEncode(Matches(z)) & chr(34) & chr(13)
    For zz = 0 to Matches(z).SubMatches.Count-1
      results = results & "Matches(" & z & ").SubMatches(" & zz & ") = " & chr(34) & Server.HTMLEncode(Matches(z).SubMatches(zz)) & chr(34) & chr(13)
    next
    results=Left(results,Len(results)-1) & chr(13)
  next
Response.Write "<pre>" & results
%>

Open in new window

0
 
LVL 4

Author Comment

by:midcompweb
ID: 22669371
excellect ddrudik, one small question...is there any way to know which of the results belongs to which tag?  The tags may not always be in the same order in the source string, and sometimes they could be completley missing from the source.

Thanks again for quick response guys
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 22669463
The way to do that would be to do the regex matches separately within an if then statement.

Set regEx = New RegExp
regEx.Global = True
regEx.IgnoreCase = True
regEx.MultiLine = True
teststring = "<your string>"
regEx.Pattern = "<title>([\S\s]*?)</title>"
Set Test = regEx.Test(teststring)
if Test=True then
  Set Matches = regEx.Execute(teststring)
  title = Matches(0).SubMatches(1)
end if
regEx.Pattern = "<meta content=""([^""]*)"" name=""keywords"" />"
Set Test = regEx.Test(teststring)
if Test=True then
  Set Matches = regEx.Execute(teststring)
  keywords = Matches(0).SubMatches(1)
end if
regEx.Pattern = "<meta content=""([^""]*)"" name=""description"" />"
Set Test = regEx.Test(teststring)
if Test=True then
  Set Matches = regEx.Execute(teststring)
  description = Matches(0).SubMatches(1)
end if
regEx.Pattern = "<meta content="<body>([\S\s]*?)</body>"
Set Test = regEx.Test(teststring)
if Test=True then
  Set Matches = regEx.Execute(teststring)
  body= Matches(0).SubMatches(1)
end if
0
 
LVL 4

Author Comment

by:midcompweb
ID: 22669559
Exactly what i was looking for, I did a little cleanup on the code so i will post the working example but thank you SO much for you help!
Set regEx = New RegExp
	regEx.Global = True
	regEx.IgnoreCase = True
	regEx.MultiLine = True
	teststring = PageContent
	regEx.Pattern = "<title>([\S\s]*?)</title>"
	Test = regEx.Test(teststring)
	if Test=True then
	  Set Matches = regEx.Execute(teststring)
	  title = Matches(0).SubMatches(0)
	end if
	regEx.Pattern = "<meta content=""([^""]*)"" name=""keywords"" />"
	Test = regEx.Test(teststring)
	if Test=True then
	  Set Matches = regEx.Execute(teststring)
	  keywords = Matches(0).SubMatches(0)
	end if
	regEx.Pattern = "<meta content=""([^""]*)"" name=""description"" />"
	Test = regEx.Test(teststring)
	if Test=True then
	  Set Matches = regEx.Execute(teststring)
	  description = Matches(0).SubMatches(0)
	end if
	regEx.Pattern = "<body>([\S\s]*?)</body>"
	Test = regEx.Test(teststring)
	if Test=True then
	  Set Matches = regEx.Execute(teststring)
	  body= Matches(0).SubMatches(0)
	end if
	
	Response.Write "Title=" & title
	Response.Write "keywords=" & keywords
	Response.Write "description=" & description
	Response.Write "body=" & body

Open in new window

0
 
LVL 4

Author Closing Comment

by:midcompweb
ID: 31504241
thanks again  :)  regular expressions have baffeled me for a long time  :)
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22669580
Thanks for the question and the points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question