Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3286352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:22:12+00:00 2026-05-17T20:22:12+00:00

I have some regular expression code that grabs the data between the title tags

  • 0

I have some regular expression code that grabs the data between the title tags on a page:

<%
    Function UrlExists(sURL)
        Dim objXMLHTTP
        Dim thePage
        Dim strPTitle   
        Dim blnReturnVal
        Dim objRegExp
        Dim strTitleResponse

        'Create object
        Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
        on error resume next

        'Get the head
        objXMLHTTP.Open "HEAD", sURL, false
        objXMLHTTP.setRequestHeader "User-Agent", Request.ServerVariables("HTTP_HOST")
        objXMLHTTP.Send ""

        '404?        
        If Err.Number <> 0 or objXMLHTTP.status <> 200 then blnReturnVal = "0|404 Error" Else blnReturnVal = "1|"
        objXMLHTTP.close

        'If not 404
        if left(blnReturnVal,1) = "1" then

            'Get the physical page
            objXMLHTTP.Open "GET", sURL, false
            objXMLHTTP.Send ""
                thePage = objXMLHTTP.responseText 
                thePage = replace(thePage, vbCrlf, "")
            objXMLHTTP.close

            'Find title
            Set objRegExp = New Regexp

            objRegExp.IgnoreCase = true
            objregexp.Multiline = true
            objRegExp.Global = false
            objRegExp.Pattern = "<title[^>]*?>(.*)</title>" 

            set strPTitle =  objRegExp.Execute(thePage)
            strTitleResponse = strPTitle.Item(0).Value
            strTitleResponse = replace(strTitleResponse, vbCrlf, "")
            strTitleResponse = trim(strTitleResponse)
            if len(strTitleResponse) <1 OR strTitleResponse = "" then strTitleResponse = "(No Title)"

            set objRegExp = nothing
            strTitleResponse = replace(strTitleResponse,"</title>","")
            strTitleResponse = replace(strTitleResponse,"<title>","")
            strTitleResponse = replace(strTitleResponse,"'","&#39; ")
            blnReturnVal = blnReturnVal & strTitleResponse

        end if

        Set objXMLHTTP = nothing

        UrlExists = blnReturnVal
    End Function
%>        

This works fine and has been for many months, but when I wrote it (stupidly?) I made the assumption each page would only have one or no title tags. It’s recently started to throw weird errors on the John Lewis page because it has two titles in it’s HTML:

    <title>John Lewis - Shop online at Britain's Favourite Retailer</title>
... bunch of html
<title>

    </title>

How can I modify the regexp to match only the first matched pair, not getting confused with the HTML above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:22:13+00:00Added an answer on May 17, 2026 at 8:22 pm

    In before all this “you should use a parser”: make you regexp non-greedy:

    objRegExp.Pattern = "<title[^>]*?>(.*?)</title>" 
    

    Notice the added ? after .*. Per default .* will match as much as possible. This behaviour is inverted with the additional ?, now matching as little as possible.

    Warning: I know absolutely nothing about regular expressions i classic ASP (or “modern” ASP, if there is such a thing), but since the non-greedy / laziness operator is already used on the <title> tag match, I reckon it will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited some code that uses regular expressions to parse CSV formatted data.
I have a regular expression (REGEX 1) plus some Perl code that picks out
I have some ActionScript code that splits a string using Regular Expression and lets
Need some help, I have a regular expression that appears to work just fine
Does anyone have code for finding a file that contains a regular expression? I
I know that MySQL does not have a regular expression replace function but I
Need some regular expressions help. So far I have my code working to allow
I have to create a regular expression for some path conversion. Example for path
im new to regular expressions in php. I have some data in which some
I have a Python library that, in addition to regular Python modules, has some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.