Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to parse a string for a sign
Message
De
23/05/2003 13:31:48
 
 
À
23/05/2003 04:46:05
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Allemagne
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00792098
Message ID:
00792332
Vues:
37
This message has been marked as the solution to the initial question of the thread.
>Hi,
>
>I try to find split a given String into parts.
>
>The string is a SELECT SQL from CURSORGETPROP('SQL',ALIAS())
>
>I like to add a CHR(13) after each field.
>
>So as far as I understand i need to replace al ',' with ','+CHR(13), but only those that are not wraped in "" '' [] ().
>
>How do I find those signs?
>
>Example:
>
>Source = "SELECT aField, bField AS Field2, [, ]+PADR(cField,2) AS cField ..."
>Result: "SELECT aField,"+CHR(13)+" bField AS Field2,"+CHR(13)+" [, ]+PADR(cField,2) AS cField ..."
>
>
>
>Agnes

Agnes,

seemed like fun to do. Does this work ?
*--------------------------------------------------------------------------
#define TRUE	.T.
#define FALSE	.F.

function do_it()

	local SearchFor, ReplaceBy
	SearchFor = ' *, *'
	ReplaceBy = ', ' +chr(13)+chr(10)
	
	?FunctionWithoutName("SELECT aField, bField AS Field2, [, ]+PADR(cField,2) AS cField ...", SearchFor, ReplaceBy )
	
	?FunctionWithoutName("select sum(a+b), sum(c+d) as eee, sum(e+f)", SearchFor, ReplaceBy )

	?FunctionWithoutName("select sum(a+b), sum(c+d) + [123['] as eee, sum(e+f)", SearchFor, ReplaceBy )
endfunc
*--------------------------------------------------------------------------
function FunctionWithoutName(Source, SearchFor, ReplaceBy)

	local RegExp, Match, i, Result, s, c, ToDo, l, n
	local RegExp2, Match2
	
	RegExp	= CreateObject('VBScript.RegExp')
	RegExp.IgnoreCase = FALSE
	
	RegExp2	= CreateObject('VBScript.RegExp')
	
	s = source
	
	&& (1) replace '', "", [] and everything between with dots
	&& [ " '
	do while TRUE
	
		RegExp.Pattern = '[' + [['"] + ']'
		
		RegExp.Global = FALSE
		Match = RegExp.Execute(s)
	
		do case
		case !empty(Match.Count)
			i = Match.Item[0].FirstIndex
			c = Match.Item[0].Value
			
			do case
			case inlist(c, ['], ["])
				RegExp.Pattern = c 
				RegExp.Global = TRUE
				Match = RegExp.Execute(s)
				
				do case
				case (Match.Count < 2)
					assert FALSE message 'expr not well formed'
					return FALSE
				
				otherwise
					l = Match.Item[1].FirstIndex - i + 1
					s = stuff(s, i+1, l, repl('.', l))
					
				endcase
			
			otherwise
				RegExp.Pattern = '\]'
				RegExp.Global = FALSE
				Match = RegExp.Execute(s)
				
				do case
				case empty(Match.Count)
					assert FALSE message 'expr not well formed'
					return FALSE
				
				otherwise
					l = Match.Item[0].FirstIndex - i + 1
					s = stuff(s, i+1, l, repl('.', l) )
					
				endcase
			endcase
		otherwise
			exit
		endcase
	enddo
	
	&& (2) ( ) nested, replace (...) with .....
	RegExp.Global = TRUE
	RegExp.Pattern = '\('
	Match = RegExp.Execute(s)
	
	RegExp2.IgnoreCase = FALSE
	RegExp2.Global = FALSE
	RegExp2.Pattern = '\)'
	
	for n = Match.Count - 1 to 0 step -1
		
		i = Match.Item[n].FirstIndex + 1
		Match2 = RegExp2.Execute( substr(s, i+1 ))
		
		do case
		case empty(Match2.Count)
			assert FALSE message 'expr not well formed'
			return FALSE
		
		otherwise
			l = Match2.Item[0].FirstIndex + 2
			s = stuff(s, i, l, repl('.', l))
		endcase
	endfor

	&& (3) Final
	RegExp.Global = TRUE
	RegExp.Pattern = SearchFor
	Match = RegExp.Execute(s)
		
	for i = Match.Count - 1 to 0 step -1
		Source = stuff(Source, Match.Item[i].FirstIndex + 1, Match.Item[i].Length, ReplaceBy)
	endfor
	
	return Source 
endfunc
*--------------------------------------------------------------------------
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform