Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran() applied selectively
Message
De
04/06/2016 16:09:59
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01636985
Message ID:
01637004
Vues:
63
>this is brilliant antonio - over my head so forgive the level of simplification I am going to approach this with.
>I actually only tried the initial line of your solution and it corrected the string (fantastic) I assume the code underneath it is explanation of the way patterntran works.
>the syntax of the code you placed at the end of my string is then what I am trying to unravel. Is it possible to keep the hyphen open as a variable, undefined character
>[.*-.*\]
>so in a nutshell to adjust the code in a way that whatever character occurs between the numbers [21-22] [21 22] [21_22] be replaced with dot. btw these could be three digit numbers as well [99~100]
>
>thanks
>k
>
>
>>>I am searching for the most expedient way to change a character in a specific area of a string.
>>>
>>>mstr='1st-title: ANAME-HER : 3520931 place[21-22]'
>>>in this string the hyphen between 21 and 22 should be a dot.
>>>if I applied chrtran() I would end up changing all the hyphens which I do not want to do.
>>>
>>>right now all I've been able to accomplish is to locate the error in a portion of the string, but putting it back together is starting to get a bit unwieldy.
>>>Is there a simpler more direct route of directing the chrtran() to the area beyond '['
>>>
>>>thanks
>>>k
>>

This is a revised version of PATTERNTRAN(), using Gregory's approach - meaning that it uses the RegExp engine capabilities to replace a match with a string that may refer to groups in the pattern (this is also gives a clear indication that the FFC wrapper class ought to be changed to accommodate for other properties and methods of the underlying engine object).

Gregory would explain this much better than me, it's his code with just a slight modification because of your new requirements, that the separator may be something other than an hyphen and digits can vary in length. To address this, the pattern is set to 3 groups, the second one referring to the separator that can be almost any character, that is, (.) - the parenthesis being used for grouping. The replacement $1.$3 means: use whatever it is in the string that matches the first group, a dot, and then whatever corresponds to the third group.
? PATTERNTRAN("1st-title: ANAME-HER : 3520931 place[21~22]", "(\[\d*)(.)(\d*\])","$1.$3")
? PATTERNTRAN("1st-title: ANAME-HER : 3520931 place[99 100]", "(\[\d*)(.)(\d*\])","$1.$3")
? PATTERNTRAN("1st-title: ANAME-HER : 3520931 place[35-36]", "(\[\d*)(.)(\d*\])","$1.$3")

* A function to translate a pattern with a string
* Arguments as main arguments in STRTRAN
FUNCTION PATTERNTRAN (tcSearched AS String, tcPatternSought AS String, tcReplacement AS String)

	LOCAL lcTranslated AS String
	LOCAL lcTranslatedMatch AS String

	* load regular expression engine
	LOCAL loRegExpr AS VBScript.RegExp

	m.loRegExpr = CREATEOBJECT("VBScript.RegExp")
	m.loRegExpr.Global = .T.
	m.loRegExpr.Ignorecase = .F.

	* the pattern we look in for inside the original string
	m.loRegExpr.Pattern = m.tcPatternSought

	m.lcTranslated = m.loRegExpr.Replace(m.tcSearched, m.tcReplacement)

	m.loRegExpr = .NULL.

	RETURN m.lcTranslated
ENDFUNC
----------------------------------
António Tavares Lopes
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform