Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chrtran() applied selectively
Message
De
04/06/2016 03:01:49
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01636985
Message ID:
01636988
Vues:
78
>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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform