webForumDet fria alternativet

RegExp - få ut siffran

ASP

6 svar · 750 visningar · startad av Mika3l

Medlem sedan nov. 20018 492 inlägg
Frågan#1

Hejsan!
Jag vill med hjälp av RegExp få ut idnumret från strängen <img]id</img] alternativt <img=id] för att sedan hämta uppgifter i en databas och visa en viss xhtml-kod.
Jag försökte med den här koden men jag får med allt och inte bara idnumret och det leder ju naturligtvis till problem vid uppkopplingen till databasen.
Är inte så speciellt insatt i RegExp så jag kanske är helt ute och cyklar med den koden jag har försökt med?

Function fixpic(strValue)

With New RegExp

  .Global = True
  .IgnoreCase = True

  .Pattern = "\[img\]([\w\W]*?)\[/img\]"
  strValue = .Replace(strValue,"$1")

End With

iid = strValue
Set rs = Connection.Execute("SELECT f_title_" & lang & " FROM tblGallery WHERE f_iid = " & iid)
If NOT rs.EOF Then
  strValue = .Replace(strValue,"<a rel=""shadowbox[content_img];options={counterType:'skip',continuous:true,animSequence:'sync'}"" href=""images/gallery/fullsize/" & iid & ".jpg"" title=""" & rs("f_title_" & lang) & """><img src=""images/gallery/thumbs/" & iid & "_thumb.jpg?cache=" & int(rnd*1000000) & """ style=""border: 1px solid #000000; margin-bottom: 5px;"" /></a>")
End If
rs.Close : Set rs = Nothing
fixpic = strValue
End Function
Medlem sedan juni 20019 519 inlägg
#2
\[img\](\d+)\[/img\]

Och

\[img=(\d+)\]

Om du endast vill träffa tal 0-9. Du kanske skall använda dig av:

\[img\]([1-9]\d*)\[/img\]

för att inte tillåta 0123 utan skall vara 123 dvs börja allt förutom 0.

Medlem sedan nov. 20018 492 inlägg
#3
.Pattern = "\[img=([1-9]\d*)\]"

Jag försöker använda mig av den koden och den plockar ut siffran rätt men xhtml-koder följer med siffran vilket gör att det blir fel i min SQL-sats sen. Får med <p>1</p>. Nåt som kan fixa till det? :)

Medlem sedan juni 20019 519 inlägg
#4

Eftersom du verkar ha annan text än just bara img biten så får du antingen ta ut alla matchningar som då får på sidan och hämta ut submatch 0 på den träffen (finns säkert bättre sätt:

Function fixpic(strValue)
	With New RegExp
	.Global = True
	.IgnoreCase = True
	.Pattern = "\[img=([1-9]\d*)\]"
	Set vMatches = myRegExp.Execute(strValue)
	For Each Match in vMatches
		iid = Match.SubMatches(0)
		Set rs = Connection.Execute("SELECT f_title_" & lang & " FROM tblGallery WHERE f_iid = " & iid)
		If NOT rs.EOF Then
			strValue = Replace(strValue, Match, "<a rel=""shadowbox[content_img];options={counterType:'skip',continuous:true,animSequence:'s  ync'}"" href=""images/gallery/fullsize/" & iid & ".jpg"" title=""" & rs("f_title_" & lang) & """><img src=""images/gallery/thumbs/" & iid & "_thumb.jpg?cache=" & int(rnd*1000000) & """ style=""border: 1px solid #000000; margin-bottom: 5px;"" /></a>")
		End If
	Next
	End With
	fixpic = strValue
End Function
Medlem sedan nov. 20018 492 inlägg
#5

Körningsfel i Microsoft VBScript (0x800A01A8)
Objekt krävs.: 'myRegExp'

? :)
Testade att ta bort "my" men det stämde tydligen inte heller.

Medlem sedan juni 20019 519 inlägg
#6

my bad:

Function fixpic(strValue)
	With New RegExp
	.Global = True
	.IgnoreCase = True
	.Pattern = "\[img=([1-9]\d*)\]"
	Set vMatches = .Execute(strValue)
	For Each Match in vMatches
		iid = Match.SubMatches(0)
		Set rs = Connection.Execute("SELECT f_title_" & lang & " FROM tblGallery WHERE f_iid = " & iid)
		If NOT rs.EOF Then
			strValue = Replace(strValue, Match, "<a rel=""shadowbox[content_img];options={counterType:'skip',continuous:true,animSequence:'s    ync'}"" href=""images/gallery/fullsize/" & iid & ".jpg"" title=""" & rs("f_title_" & lang) & """><img src=""images/gallery/thumbs/" & iid & "_thumb.jpg?cache=" & int(rnd*1000000) & """ style=""border: 1px solid #000000; margin-bottom: 5px;"" /></a>")
		End If
	Next
	End With
	fixpic = strValue
End Function
Medlem sedan nov. 20018 492 inlägg
#7

Fungerar utmärkt! Man tackar! :)

566 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
366 ms — hämta tråd, inlägg och bilagor (db)
197 ms — ändringar (db)