webForumDet fria alternativet

RegExp problem

12 svar · 419 visningar · startad av finalworld

finalworldMedlem sedan maj 200286 inlägg
#1

Hej, jag har ett problem.
Jag bygger ett forum där man ska kunna skriva aspkod
Koden blir då färgkodad vilket fungerar jätte bra
jag kör typ såhär

<%
Response.Write("Hello World!")
%>

Jag har nu TVÅ problem jag inte lyckas lösa
1. jag vill att bara det mellan code taggarna ska bli färgkodade, inte resten av inlägget
2. Jag vill att koden mellan code taggarna ska få radnummer typ
001:
002:
003:
osv

Tacksam för svar
Mvh
//FinalWorld

Jesper TMedlem sedan nov. 20017 058 inlägg
#2

Radnummer har jag ingen aning om hur du fixar, men codetaggarna kan du kanske lösa så här:

<%
inpstr="[code]&lt;%"& vbcrlf &"Response.Write("Hello World!")"& vbcrlf &"%&gt;[/code]"
inpstr=Replace(inpstr,"[/code]","</pre>")
inpstr=Replace(inpstr,"[code]","<pre style='color:red;'>")
inpstr=Replace(inpstr,vbcrlf,"<br>")
Response.write inpstr
%>

Efter en liten sökning så hittade jag detta:
http://www.webforum.nu/showthread.php?s=&postid=524616#post524616
Javascript dock.

finalworldMedlem sedan maj 200286 inlägg
#3

Men det fungerar ju ej
Jag har en include fil med massa RegExp i
som ändrar tex Response till en röd text
hur får jag då ihop det ?

Jesper TMedlem sedan nov. 20017 058 inlägg
#4

Que??
Visa regexpkoden, så kanske man kan göra ngt i den då.
Eller hur menar du?

finalworldMedlem sedan maj 200286 inlägg
#5

Det är ganska så mycet tyvär....

Function Forum_ChangeCode(strText)

strText = Replace(server.HTMLEncode(strText&""),vbCrlf,"<br>")

Set objRegExp = New regexp
objRegExp.Global = true
objRegExp.IgnoreCase = true


' Mösnter som ersätter mellanslag
objRegExp.Pattern = "\\40{2,}"
strText = objRegExp.Replace(strText,"&nbsp;&nbsp")

' Mönster som fixar tabar
objRegExp.Pattern = "\\11"
strText = objRegExp.Replace(strText,"&nbsp;&nbsp&nbsp;&nbsp")

' CODE

objRegExp.Pattern="\\\[code\\\](.\*?)\\\[/code\\\]"
strText = objRegExp.Replace(strText,"\<font style=""font-family: Courier New, Courier, mono;font-Size:11px;"" \>$1\</font\>")

' Include
objRegExp.Pattern="\\&lt;!-- #include file = &quot;(.\*?)\\&quot; --&gt;"
strText = objRegExp.Replace(strText,"\<font color='#666666'\>&lt;!--#include file = &quot;$1&quot; --&gt;\</font\>")
objRegExp.Pattern="\\&lt;!--#include file = &quot;(.\*?)\\&quot; --&gt;"
strText = objRegExp.Replace(strText,"\<font color='#666666'\>&lt;!--#include file = &quot;$1&quot; --&gt;\</font\>")
objRegExp.Pattern="\\&lt;!-- #include virtual = &quot;(.\*?)\\&quot; --&gt;"
strText = objRegExp.Replace(strText,"\<font color='#666666'\>&lt;!--#include file = &quot;$1&quot; --&gt;\</font\>")
objRegExp.Pattern="\\&lt;!--#include virtual = &quot;(.\*?)\\&quot; --&gt;"
strText = objRegExp.Replace(strText,"\<font color='#666666'\>&lt;!--#include file = &quot;$1&quot; --&gt;\</font\>")		

strText = Replace(strText,"**","\<strong\>")
strText = Replace(strText,"**","\</strong\>")
strText = Replace(strText,"*","\<em\>")
strText = Replace(strText,"*","\</em\>")
objRegExp.Pattern="\\&lt; \*\[bB\] \*&gt;(.\*?)\\&lt;/\[bB\] \*&gt;"
strText = objRegExp.Replace(strText,"\<strong\>$1\</strong\>")	
objRegExp.Pattern="\\&lt; \*\[iI\] \*&gt;(.\*?)\\&lt;/\[iI\] \*&gt;"
strText = objRegExp.Replace(strText,"\<em\>$1\</em\>")			    	
objRegExp.Pattern="\\&lt; \*font \*color \*= \*&quot;\*(.\*?)&quot;\* \*&gt;(.\*?)\\&lt;/font&gt;"
strText = objRegExp.Replace(strText,"\<font color=""$1""\>$2\</font\>")

	    	objRegExp.Pattern = "&lt;%"
	    	strText = objRegExp.Replace(strText, "\<font style=""BACKGROUND-COLOR: #cccccc""\>&lt;%\</font\>")
	    	objRegExp.Pattern = "%&gt;"
	    	strText = objRegExp.Replace(strText, "\<font style=""BACKGROUND-COLOR: #cccccc""\>%&gt;\</font\>")
	    	
	    	
	    	' Färg RÖD
	    	objRegExp.Pattern = "request"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Request\</font\>")
	    	objRegExp.Pattern = "response"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Response\</font\>")
	  	objRegExp.Pattern = "querystring"
	   	strText = objRegExp.Replace(strText, "\<font color=red\>Querystring\</font\>")
	     	objRegExp.Pattern = "write"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Write\</font\>")
	    	objRegExp.Pattern = "redirect"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Redirect\</font\>")
	    	objRegExp.Pattern = "Buffer"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Buffer\</font\>")		    	

	    	
	    	objRegExp.Pattern = "Session"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>Session\</font\>")	
	    	objRegExp.Pattern = ".form"
	    	strText = objRegExp.Replace(strText, "\<font color=red\>.Form\</font\>")	
	    			    			    				
		' Färg BLÅ
		
		objRegExp.Pattern = "ServerVariables"
	   	strText = objRegExp.Replace(strText, "\<font color=blue\>ServerVariables\</font\>")	
	    	objRegExp.Pattern = "execute"
	   	strText = objRegExp.Replace(strText, "\<font color=blue\>Execute\</font\>")			
	    	objRegExp.Pattern = "nothing"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>Nothing\</font\>")
	    	objRegExp.Pattern = "set"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>Set\</font\>")				
	    	objRegExp.Pattern = "now"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>Now\</font\>")	
	    	objRegExp.Pattern = "if"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>If\</font\>")
	    	objRegExp.Pattern = "end"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>End\</font\>")
	    	objRegExp.Pattern = "else"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>Else\</font\>")
	    	objRegExp.Pattern = "then"
	    	strText = objRegExp.Replace(strText, "\<font color=blue\>Then\</font\>")
	    	
	    				
		' Färg GRÖN
	    	objRegExp.Pattern = "select"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Select\</font\>")
	    	objRegExp.Pattern = "update"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Update\</font\>")
	    	objRegExp.Pattern = "insert into"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Insert Into\</font\>")
	    	objRegExp.Pattern = "where"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Where\</font\>")
	    	objRegExp.Pattern = "from"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>From\</font\>")	    	
	    	objRegExp.Pattern = "VALUES"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Values\</font\>")	
	    	objRegExp.Pattern = "DELETE"
	    	strText = objRegExp.Replace(strText, "\<font color=green\>Delete\</font\>")			    	
	    	
	    	
	    	 	
		' Färg LILA
	    	objRegExp.Pattern = "close"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>Close\</font\>")
	    	objRegExp.Pattern = "eof"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>Eof\</font\>")	
	    	objRegExp.Pattern = "bof"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>Bof\</font\>")
	    	objRegExp.Pattern = "Option Explicit"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>Option Explicit\</font\>")
	    	objRegExp.Pattern = "recset"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>RecSet\</font\>")
	    	objRegExp.Pattern = "Option Explicit"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>Option Explicit\</font\>")		    			    	
	    	objRegExp.Pattern = "uBound"
	    	strText = objRegExp.Replace(strText, "\<font color='#990099'\>uBound\</font\>")			    			    		

Forum_ChangeCode = strText
End Function

finalworldMedlem sedan maj 200286 inlägg
#6

ingen som vet äns hur jag ska göra så endast koden mellan

 och

blir färgar och inte de andra ...

J.N.Medlem sedan apr. 20031 660 inlägg
#7

HEJ!

Jag ger mig på den andra biten, med pseduo-kod, då det är lättare :r

Min tanke på radnumreringen är att varje rad slutar med vbcrlf, eller hur? För att få ihop detta behövs alltså en kontroll av varje vbcrlf, en variabel som skrivs ut/läggs till i strängen? och ökas med ett.

Eller?

finalworldMedlem sedan maj 200286 inlägg
#8

så sant
och det är kanske inte så svårt
men att få det som är mellan code taggarna jag har svårt med.

J.N.Medlem sedan apr. 20031 660 inlägg
#9

HEJ!

Tittade igenom ditt inlägg för att se om jag fattade det bättre nu, och kanske :stud

Det enkla är ju att få ditt regexp att endast köra mellan code-taggarna, men det har du säkert listat ut.

Alternativ två är att plocka ut texten mellan code-taggarna, formatera om den, nästa code-taggar, formatera om den osv och efterhand lägga till bef (obehandlad) text till den behandlade tills du kommer till sluttexten i inlägget.

Hjälpte det?

finalworldMedlem sedan maj 200286 inlägg
#10

Det enkla är ju att få ditt regexp att endast köra mellan code-taggarna, men det har du säkert listat ut.

nopp, det har jag ej :(

lbrinkMedlem sedan aug. 200186 inlägg
#11

objRegExp.Pattern = "(close|Option Explicit|eof|bof|recset|uBound)"
strText = objRegExp.Replace(strText, "<font color='#990099'>$1</font>")

objRegExp.Pattern = "(request|response|querystring|write|redirect|Buffer|Session|.form)"
strText = objRegExp.Replace(strText, "<font color=red>$1</font>")

objRegExp.Pattern = "(ServerVariables|Execute|Nothing|Set|Now|If|End|Else|Then)"
strText = objRegExp.Replace(strText, "<font color=blue>$1</font>")

objRegExp.Pattern = "(select|DELETE|VALUES|where|from|insert into|update)"
strText = objRegExp.Replace(strText, "<font color=green>$1</font>")

Här har du en lite mer optimerad kod av färdkodningen.

Jesper TMedlem sedan nov. 20017 058 inlägg
#12

Appropå kod-taggar. Använd dem i detta forum också. ;)

finalworldMedlem sedan maj 200286 inlägg
#13

lbrink:

Tack så hämskt mycket :)
jag är inte alls bra på regexp och mitt fungerade ju, men har nu din kod, den va ju klart bättre.

Men den löste ändå inte mitt problem
nu har jag byggt detta för att kunna ändra färgen på det som är mellan [ code] taggarna, men det vill inte fungera, någon som ser felet

With New RegExp
.Pattern="\[code\][\w\W]+\[/code\]"
.IgnoreCase=True
.Global=True
Set objMatches=.Execute(strText)
strResult=""
lngPos=1
For each objMatch in objMatches
If objMatch.FirstIndex>lngPos Then strResult=strResult & Mid(strText, lngPos, objMatch.FirstIndex-lngPos+1)
strCode=objMatch.Value

	objRegExp.Pattern = "(close\|Option Explicit\|eof\|bof\|recset\|uBound)"
	strText = objRegExp.Replace(strText, "\<font color='#990099'\>$1\</font\>")
	
	objRegExp.Pattern = " (request\|response\|querystring\|write\|redirect\|Buffer\|Session\|.form)"
	strText = objRegExp.Replace(strText, "\<font color=red\>$1\</font\>")
	
	objRegExp.Pattern = "(ServerVariables\|Execute\|Nothing\|Set\|Now\|If\|End\|Else\|Then)"
	strText = objRegExp.Replace(strText, "\<font color=blue\>$1\</font\>")
	
	objRegExp.Pattern = "(select\|DELETE\|VALUES\|where\|from\|insert into\|update)"
	strText = objRegExp.Replace(strText, "\<font color=green\>$1\</font\>")	

    strResult=strResult & strCode
    lngPos=objMatch.FirstIndex+objMatch.Length+1
Next
strText=strResult & Mid(strText, lngPos)

End With

Genererad på 384 ms · cache AV · v20260730165559-full.f96bc7eb