Showing posts with label Regular Expression. Show all posts
Showing posts with label Regular Expression. Show all posts

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

Thursday, May 1, 2008

Foxpro Regular Expression

Below code converts Sql code generated for Insert from Management studio into Foxpro code.
Right click on a table in SSMS -> Script table As -> Insert To -> New Query Editor Window

Below was generated from Publishers table from Pubs database

INSERT INTO [pubs].[dbo].[publishers]
([pub_id]
,[pub_name]
,[city]
,[state]
,[country])
VALUES
(<pub_id, char(4),>
,<pub_name, varchar(40),>
,<city, varchar(20),>
,<state, char(2),>
,<country, varchar(30),>)


Select all & Copy

In foxpro command window
lcStr = _cliptext
This will put the generated code in clipboard.



loR = CREATEOBJECT('vbscript.regexp')
lor.Global = .t.
lor.Multiline = .t.
lor.IgnoreCase = .t.
lor.Pattern = "(,\s\w+\(?\d*\)?,\>)"
lcstr = lor.Replace(lcSTr, '')
lcstr = STRTRAN(lcStr, '<', '?')

This is what will be the value of lcStr after executing above code

INSERT INTO [pubs].[dbo].[publishers]
([pub_id] ,[pub_name] ,[city] ,[state] ,[country])
VALUES
(?pub_id ,?pub_name ,?city ,?state ,?country)