Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WITHOUT regular expressions! 1

Status
Not open for further replies.

whinger74

Programmer
Aug 13, 2002
25
GB
Hi,

I am trying to get the value between the title tags of a page into a variable. I thought my host's server had the most up to date version of VBscript engine, but it is not letting me do this task using Regular Expressions. (I am creating a site search, and have the file contents in a textstream, using FSO). Does anyone know of a way of doing this WITHOUT using regular expressions? I am starting to get a little frustrated...
Many thanks in advance
 
Code:
'assume pageContent variable has string contents of page
Dim titleStr
titleStr = mid(pageContent,(InStr(pageContent,&quot;<title>&quot;)+7),(InStr(pageContent,&quot;</title>&quot;)-InStr(pageContent,&quot;<title>&quot;)-7)
Give that a shot, a little complicated, but I decided to se if I could do it in one line(not counting declarations)
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
I intially got the following error:


Microsoft VBScript compilation error '800a03ee'

Expected ')'

build_searchindex.asp, line 48

title>&quot;)-InStr(strContents,&quot;<title>&quot;)-7)
----------------------------------------^


so I inserted a ) for the hell of it, and then got:



Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument: 'Mid'

build_searchindex.asp, line 48


Do you have any thoughts, Tarwn?
 
Hmm, thats generally only a problem, if the last variable(length) plus the second variable (starting position) exceeds the length of the string. Or if either of the values is null. Instead of doing it all in one line like above, try breaking it down like so:
Code:
Dim startPos, endPos, titleStr
startPos = InStr(pageContent,&quot;<title>&quot;)+7
endPos = InStr(pageContent,&quot;<title>&quot;)
Response.Write &quot;Start: &quot;&startPos & &quot;<br>End:&quot;&endPos
'titleStr = mid(pageContent,startPos,endPos-startPos)
One possibility here is that your title tags are not capitilized the same way. By breaking it down like this you have the option for looking first for lowercase, then uppercase title tags.
Code:
Dim startPos, endPos
startPos = InStr(1,pageContent,&quot;<title>&quot;,1)+7
endPos = InStr(1,pageContent,&quot;<title>&quot;,1)

If endPos - startPos > 0 Then
	titleStr = mid(pageContent,startPos,endPos - startPos)
End If

Response.Write titleStr
Sorry, there is a case insensitive search.
InStr(start position,String,String,case sensitive)
By default InStr is case sensitive, so by puttin g a 1 in the 4th argument we have made it case insensitive. Something interesting I just found is that you have to declare the position (first argument) when declaring sensitivity, otherwise it assumes (seeing three arguments) that the string in the first argument is in erro and was supposed to be a position.
Hope that helped. It's still possible to write it in one line, but may be safe to leave it seperated out.
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
Many thanks Tarwn! This now works

I'm still fairly new to InStr, and there is nothing in my VB/ASP books on it, and next to nothing on the web either...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top