Jump to content

How to search for any 3 digits and delete them


Recommended Posts

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

Link to comment
Share on other sites

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 ==> linx

ann777nna ==> annnna

l)ark_473 ==> l)ark_

1231ff ==> 1ff

If 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  

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by 99ojo
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

An example without creating an array.

Local $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 by Malkey
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...