Monday, May 5, 2008

Remove line numbers from code listing

Sometime code listed on web page may contain line numbers. I have used regular expression to remove the line numbers from the code.

LPARAMETERS tcString

LOCAL loRegex as "vbscript.regexp"
LOCAL ARRAY laLines[1]
LOCAL lnLines, lcLine, lcStr, lni

loRegex = CREATEOBJECT('vbscript.regexp')
loRegex.GLOBAL = .T.
loRegex.IgnoreCase = .F.
loRegex.Multiline = .T.

IF EMPTY(tcString)
tcString = _CLIPTEXT
ENDIF

lnLines = ALINES(laLines, tcString)

lcStr = ""
loRegex.PATTERN = "^\s*(\d+:?)(.*)"
FOR lni = 1 TO lnLines
lcLine = laLines[lnI]
IF EMPTY(lcLine)
*!* lcStr = lcStr + lcLine
ELSE
lcStr = lcStr + loRegex.REPLACE(lcLine, "$2") + CHR(13) + CHR(10)
ENDIF
ENDFOR


WAIT CLEAR
CREATE CURSOR curtmpfile (CODE m)
INSERT INTO curtmpfile VALUES (lcStr)

MODIFY MEMO CODE

RETURN

No comments: