techshots Posted March 22, 2010 Posted March 22, 2010 Hi, My goal is to find any 3 digits in a file called "rite_results.rtf", and delete them all. I've got the following script but this line doesn't seem to work: $replace_string_1 = _ReplaceStringInFile ($sFile_results, '([0-9]{0,3})', "test") Can someone tell me what's wrong? source file "3.txt" is attached. script: ------ #include <string.au3> #include <array.au3> #include <File.au3> ; must use rtf extension to retain data inserted one per line $sFile_0 = "C:\Users\HN\Desktop\HIEN\A_AUTOIT\text_manipulation\write_0.rtf" $sFile_1 = "C:\Users\HN\Desktop\HIEN\A_AUTOIT\text_manipulation\write_1.rtf" $sFile_results = "C:\Users\HN\Desktop\HIEN\A_AUTOIT\text_manipulation\write_results.rtf" dim $array1 dim $array_0 dim $array_1 ; read file into array : note file has no commas only each data per line _FileReadToArray("C:\Users\HN\Desktop\HIEN\A_AUTOIT\text_manipulation\3.txt", $array1) ; insert geographic area codes 0 = east 1 = west plus comma $string1 = _arraytostring($array1, @CRLF & "0,") $string2 = _arraytostring($array1, @CRLF & "1,") FileWrite($sFile_0, $string1) FileWrite($sFile_1, $string2) ; read the two files created above in put into arrays _FileReadToArray($sFile_0, $array_0) _FileReadToArray($sFile_1, $array_1) ; insert data from array_1(source) into array_o(target) _ArrayConcatenate($array_0,$array_1) _ArrayDisplay($array_0, "array_0") _FileWriteFromArray($sFile_results, $array_0) $replace_string_1 = _ReplaceStringInFile ($sFile_results, '([0-9]{0,3})', "test") MsgBox(0, "replace_string_1", $replace_string_1)3.txt
blakel Posted March 22, 2010 Posted March 22, 2010 To restate the goal, you would like to: 1) read in the file 3.txt 2) replace every occurrence of 3 consecutive numbers with 'test' 3) write the file out
BugFix Posted March 22, 2010 Posted March 22, 2010 You want to delete all occurences of 3 digits at the end of a word, at beginning or also inside?In your text you have:linx562 ==> linxann777nna ==> annnnal)ark_473 ==> l)ark_1231ff ==> 1ffIf you want cut every last 3 digits you can do so:$read = FileRead('Your_File') $fh = FileOpen('Your_File', 2) FileWrite($fh, StringRegExpReplace($str, '\d{0,3}(?=\r\n)', '')) FileClose($fh) Best Regards BugFix
99ojo Posted March 22, 2010 Posted March 22, 2010 Hi, this replaces every occurence of 3 digits anywhere in your string. #include <file.au3> #include <array.au3> ; only needed for _ArrayDisplay at end Global $test ;read file into array $artest _FileReadToArray (@ScriptDir & "\3.txt", $artest) For $i = 1 to UBound ($artest) - 1 ;Find any combination of 3 straight following digits and replace them with TEST $artest [$i] = StringRegExpReplace ($artest [$i], "(\d{3})", "TEST") Next _ArrayDisplay ($test) ;-)) Stefan
rameshkumar Posted March 22, 2010 Posted March 22, 2010 I Guess, _ReplaceStringInFile() function won't consider RegEx pattern. That causes your problem. First replace characters using StringRegExpReplace() function and then write to new file will rectify ur issue. -cheers ramesh
techshots Posted March 22, 2010 Author Posted March 22, 2010 Hi everyone, I'm trying to delete only lines that have 3 digits in them. any other text that has any combination of alph + number, I don't want to delete. thanks
99ojo Posted March 22, 2010 Posted March 22, 2010 (edited) Hi, for deletion: #include <file.au3> #include <array.au3> Global $test _FileReadToArray (@ScriptDir & "\3.txt", $test) For $i = UBound ($test) - 1 To 1 Step - 1 ;Find any combination of 3 straight following digits and if found, delete entry from array ;for further details have a look into helpfile If StringRegExp ($test [$i], "(\d{3})", 0) Then _ArrayDelete ($test, $i) Next _ArrayDisplay ($test) ;-)) Stefan Edited March 22, 2010 by 99ojo
techshots Posted March 22, 2010 Author Posted March 22, 2010 Hi, for deletion: #include <file.au3> #include <array.au3> Global $test _FileReadToArray (@ScriptDir & "\3.txt", $test) For $i = UBound ($test) - 1 To 1 Step - 1 ;Find any combination of 3 straight following digits and if found, delete entry from array ;for further details have a look into helpfile If StringRegExp ($test [$i], "(\d{3})", 0) Then _ArrayDelete ($test, $i) Next _ArrayDisplay ($test) ;-)) Stefan Thank you very much everyone and Stefan, this worked great. Is it possible to get rid of the line count number that arrays usually insert in the beginning of any results?
99ojo Posted March 22, 2010 Posted March 22, 2010 Hi, yes and no. 1) Some functions, like StringSplit, having an option which disables the return count. 2) Just delete 1st element of array after function call You have to read the function description and write your code for that bahaviour. So my loop ends with element 1, 0 would be the return count and is not necessary for the code in between loop. ;-)) Stefan
Malkey Posted March 25, 2010 Posted March 25, 2010 (edited) An example without creating an array. expandcollapse popupLocal $sStr = "March-15" & @CRLF & _ "0_0-c" & @CRLF & _ "12_5so-da" & @CRLF & _ "sC2_Coming_Soon" & @CRLF & _ "jeff978" & @CRLF & _ "soeu" & @CRLF & _ "876kaisertek" & @CRLF & _ "xX(O_o)Xx" & @CRLF & _ "Go.0dP4lay7er" & @CRLF & _ "2Samm3y(J)7" & @CRLF & _ "youkhurt999" & @CRLF & _ "lucas_cs2" & @CRLF & _ "Tap.OuT(tF" & @CRLF & _ "mp3master(00)" & @CRLF & _ "tide2468wars" & @CRLF & _ "O.2nz" & @CRLF & _ "tjdwn5071" & @CRLF & _ "mi456nganin-1" & @CRLF & _ "nicegml.00e3" & @CRLF & _ "ddr1rt" & @CRLF ;Local $sStr = FileRead ("3.txt") ; 3 or more digits next to each other in a line. Local $sRet = StringStripWS(StringRegExpReplace($sStr, "(?m).*?\d{3}.*?$", ""), 7) ; OR ; 3 or more digits existing anywhere within a line. Local $sRet1 = StringStripWS(StringRegExpReplace($sStr, "(?m).*?(\d.*?){3}$", ""), 7) MsgBox(0, "Results", "===========================================" & @CRLF & _ "Original string :-" & @CRLF & @CRLF & $sStr & @CRLF & _ "===========================================" & @CRLF & _ "If 3 digits are next to each other then that line is removed. :-" & @CRLF & @CRLF & $sRet & @CRLF & @CRLF & _ "===========================================" & @CRLF & _ "If 3 digits anywhere in line then that line is removed :-" & @CRLF & @CRLF & $sRet1 & @CRLF & @CRLF & _ "===========================================" & @CRLF) Edited March 25, 2010 by Malkey
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now