Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Replace character expression
Message
From
09/07/2010 17:45:44
 
 
To
09/07/2010 15:22:24
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01471799
Message ID:
01472095
Views:
26
>>>>>>is there a quick way to inspect a text document and change all [1] to [01] and all [2] to [02] etc something along the lines of what i have set up below (which doesn't work) - or should i be looking at another approach altogether?
>>>>>>
>>>>>>cFile = Filetostr("C:\mytext.txt") 
>>>>>>
>>>>>>cFile = Strtran(cFile,Chr(202),"E") && using this code i can change only one character at a time
>>>>>>
>>>>>>cFile = strtran(cFile,"[5]","[05]")  && can i use the same approach for character string - perhaps using chrtran??
>>>>>>
>>>>>
>>>>>If:
>>>>>
>>>>>- you don't use the "[" character anywhere else
>>>>>- you always want to put a "0" after it, no matter the length of the number between "[" and "]"
>>>>>
>>>>>then you could just use STRTRAN() to replace each "[" with "[0".
>>>>
>>>>unfortunately al this is not the case - i have numbers 01-99 and this only affects the first 9 numbers in which case i want to see 01 and not 1 to maintain the character count. past 09 i'm fine.
>>>>
>>>>abcdefg [01] = is what i need
>>>>as opposed to submitted data which reads # abcdefg [1]
>>>
>>>Well, in that case you could just brute-force it in 10 lines of code, no RegEx needed, maintainable in pure VFP:
>>>
>>>cFile = strtran(cFile,"[0]","[00]") && if you need to handle [0]
>>>cFile = strtran(cFile,"[1]","[01]")
>>>cFile = strtran(cFile,"[2]","[02]")
>>>cFile = strtran(cFile,"[3]","[03]")
>>>cFile = strtran(cFile,"[4]","[04]")
>>>cFile = strtran(cFile,"[5]","[05]")
>>>cFile = strtran(cFile,"[6]","[06]")
>>>cFile = strtran(cFile,"[7]","[07]")
>>>cFile = strtran(cFile,"[8]","[08]")
>>>cFile = strtran(cFile,"[9]","[09]")
>>>
>>>* or, if you wanted to do it in "one line of code", you could nest the above calls < eg >
>>>
>>>Some people might pooh-pooh brute-force solutions, but they have their place.
>>
>>i was trying to go that route Al - see my example above - but unfortunately this method does not seem to be working here and i suspect it is because the strtran can only be applied to a singel character change not multiple characters. thanks for the try. k
>
>I saw your original example, I copied/pasted it for mine :)
>
>I just did a test, multi-character replacement works fine for me:
>
>lcTest = "aa [1] bb [2] cc [3] aa [1] bb [2] cc [3]"
>lcChanged = STRTRAN( lcTest, "[1]", "[01]" )
>lcChanged = STRTRAN( lcChanged, "[2]", "[02]" )
>lcChanged = STRTRAN( lcChanged, "[3]", "[03]" )
>?lcChanged
>
yes i'm sorry Al - i made the same mistake with other solutions that were offered. k
Previous
Reply
Map
View

Click here to load this message in the networking platform