Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran() applied selectively
Message
De
04/06/2016 14:32:59
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01636985
Message ID:
01637002
Vues:
64
It would have to use Gregory's approach, for that. I'll come back to this later on...

[sorry, hit "Send" inadvertently...]


>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
>>
>>A general function to perform translation based on pattern match:
>>
>>
>>? PATTERNTRAN("1st-title: ANAME-HER : 3520931 place[21-22]", "\[.*-.*\]",".","-")
>>
>>* A function to translate a pattern with a string
>>* Arguments as main arguments in STRTRAN
>>* A fourth optional argument allows to replace inside the match, only
>>FUNCTION PATTERNTRAN (tcSearched AS String, tcPatternSought AS String, tcReplacement AS String, tcSoughtInMatch AS String)
>>
>>	LOCAL lcTranslated AS String
>>	LOCAL lcTranslatedMatch AS String
>>
>>	* load regular expression engine
>>	IF !"\_REGEXP.VCX" $ SET("Classlib")
>>		SET CLASSLIB TO (ADDBS(HOME(1)) + "ffc\_regexp.vcx") ADDITIVE
>>	ENDIF
>>
>>	LOCAL loRegExpr AS _regexp
>>
>>	m.loRegExpr = CREATEOBJECT("_regexp")
>>	m.loRegExpr.Pattern = m.tcPatternSought
>>	
>>	* check if there is a match
>>	IF m.loRegExpr.Execute(m.tcSearched,.F.) = 1
>>
>>		* if we want to replace inside the match, only
>>		IF PCOUNT() = 4
>>			* perform a standard translation inside the match
>>			m.lcTranslatedMatch = STRTRAN(m.loRegExpr.Matches[1,2], m.tcSoughtInMatch, m.tcReplacement)
>>			* and then apply the translation to the match inside the original string
>>			m.lcTranslated = STRTRAN(m.tcSearched, m.loRegExpr.Matches[1,2], m.lcTranslatedMatch)
>>		ELSE
>>			* otherwise, just replace the match with the replacement
>>			m.lcTranslated = STRTRAN(m.tcSearched, m.loRegExpr.Matches[1,2], m.tcReplacement)
>>		ENDIF
>>
>>	ELSE
>>	
>>		* with no pattern found, the string is returned unstranslated
>>		m.lcTranslated = m.tcSearched
>>	
>>	ENDIF
>>	
>>	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