Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
RegEx
Message
De
23/10/2012 02:47:10
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Re: RegEx
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01555501
Message ID:
01555506
Vues:
135
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>I've figured out the first part:
>
>
>lcString = 'abc1234567890123456fd'
>
>loRegExp = CREATEOBJECT("VBScript.Regexp")
> 
>loRegExp.PATTERN = "\D\d{16}\D*" && search for non digit following by 16 digits
> 
>loRegExp.IgnoreCase = .T.
> 
>loRegExp.GLOBAL = .T.
> 
>loMatches = loRegExp.Execute(lcString)
> 
>lnMatches = loMatches.Count   && Number of matches
> 
>FOR lnMatch = 0 TO (lnMatches - 1)
> 
>      ?loMatches.Item(lnMatch).FirstIndex && Start position
>      ?loMatches.Item(lnMatch).value
> 
>ENDFOR 
>
>
>I am looking for a way to identify a 16 digit number preceeding by any non-digit character and ending with non-digit character or nothing. The pattern I defined seems to work. However, I need to replace only that 16 digit number with the 16X. I am not sure how to apply Replace command here.
>
>Thanks in advance.

if your string is 'abc1234567890123456', ie end of line after the 16 digits, it won't match
I modified the pattern so that the non digit after the 16 also matches the end of the string

Also, note that I have inserted parentheses in the pattern, to group
a) the non digit before the 16 digits
b) the 16 digits
c) the non digit after the 16 digits

I will use those groups of parentheses below
loRegExp = CREATEOBJECT("VBScript.Regexp")
 
loRegExp.PATTERN = "(\D)(\d{16})(\D|$)" && search for non digit following by 16 digits
 
loRegExp.IgnoreCase = .T.            && In this case, IgnoreCase = false is sufficient
 
loRegExp.GLOBAL = .T.
 
lcString = 'abc1234567890123456fd'
lcString = 'abc1234567890123456'
?m.loRegExp.Test(m.lcString)
(1) you can use replace() - see below - see also page 19 first row http://www.atoutfox.org/modules/articles/pdf/0000000748.pdf
&& With replace()

&& if is optional
if( m.loRegExp.Test(m.lcString) )
	? 'old: ', m.lcString 
	lcString = m.loRegExp.Replace(m.lcString, '$1$2X$3')
	
	? 'new: ', m.lcString 
endif
(2) Using Execute() is also possible and it gives you finer control

Do not forget, however, to loop the collection backwards

Using the SubMatches collection - see pages 8 and 9
&& or with Execute()
lcString = 'abc1234567890123456'

matchesObj = m.loRegExp.Execute(m.lcString)

? 'old: ', m.lcString 

for i = m.matchesObj.Count - 1 to 0 step -1
	match = m.matchesObj.item(m.i)
	
	
	
	?match.Submatches.Count
	?match.Submatches[0]
	?match.Submatches[1]
	?match.Submatches[2]

	s = m.match.Submatches[0] + m.match.Submatches[1] + 'X' + match.Submatches[2]
	lcString = stuff(m.lcString, m.match.FirstIndex+1, m.match.Length, m.s)
	
	
endfor
? 'new: ', m.lcString 
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform