---
title: "RegExp problem"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/96339-regexp-problem"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "finalworld"
published: "2004-02-02T12:09:10.000Z"
updated: "2004-02-03T07:32:04.000Z"
replies: 12
views: 432
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/96339-regexp-problem"
---

# RegExp problem

## #1 — finalworld, 2004-02-02T12:09Z

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

Permalänk: https://www.webforum.nu/p/96339

## #2 — Jesper T, 2004-02-02T12:42Z

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.

Permalänk: https://www.webforum.nu/p/1264613

## #3 — finalworld, 2004-02-02T12:49Z

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 ?

Permalänk: https://www.webforum.nu/p/1264615

## #4 — Jesper T, 2004-02-02T12:54Z

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

Permalänk: https://www.webforum.nu/p/1264620

## #5 — finalworld, 2004-02-02T12:56Z

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

Permalänk: https://www.webforum.nu/p/1264621

## #6 — finalworld, 2004-02-02T15:42Z

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

```
 och
```

 blir färgar och inte de andra ...

Permalänk: https://www.webforum.nu/p/1264723

## #7 — J.N., 2004-02-02T16:21Z

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?

Permalänk: https://www.webforum.nu/p/1264744

## #8 — finalworld, 2004-02-02T16:32Z

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

Permalänk: https://www.webforum.nu/p/1264750

## #9 — J.N., 2004-02-02T17:11Z

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?

Permalänk: https://www.webforum.nu/p/1264762

## #10 — finalworld, 2004-02-02T17:32Z

> 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 :(

Permalänk: https://www.webforum.nu/p/1264766

## #11 — lbrink, 2004-02-02T21:03Z

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.

Permalänk: https://www.webforum.nu/p/1264852

## #12 — Jesper T, 2004-02-02T21:19Z

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

Permalänk: https://www.webforum.nu/p/1264864

## #13 — finalworld, 2004-02-03T07:32Z

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

Permalänk: https://www.webforum.nu/p/1264978

---

Tråden på webben: https://www.webforum.nu/amne/asp/96339-regexp-problem
