Jump to content

Replace characters from right side of the string


Go to solution Solved by Andreik,

Recommended Posts

I have spend several hours searching the web and reading the AutoIt forum but have no idea how to find a way. I have a long list like

GROUP_hmz.tmp___\hmz7______________0035
GROUP_hmz.tmp___\hmz7TT_______0035__10
AAAALLLL\ainm1___all_____falef__________0075
AAAALLLL\ainm31___all_____falef_______0075_22
ALL_1_\tem.Qm_________0102
ALL_1_\temQ1_________0111
NONE_2_All.t__\amm.rfw_______________0551_21
NONE_2_All.t__\fem.bb__back_________0554

I want to replace the last part of each string containing several consecutive underscores "_" along with the ending digits with this new string
"-------"&$new_number
where $new_number increments by 1. I don't want to replace other underscores in the string. For example I want to change 
AAAALLLL\ainm31___all_____falef_______0075_22 
to
AAAALLLL\ainm31___all_____falef-------0001

Is it possible with AutoIt?

 

Link to comment
Share on other sites

  • Solution

Something like this?

Local $String[8]

$String[0] = 'GROUP_hmz.tmp___\hmz7______________0035'
$String[1] = 'GROUP_hmz.tmp___\hmz7TT_______0035__10'
$String[2] = 'AAAALLLL\ainm1___all_____falef__________0075'
$String[3] = 'AAAALLLL\ainm31___all_____falef_______0075_22'
$String[4] = 'ALL_1_\tem.Qm_________0102'
$String[5] = 'ALL_1_\temQ1_________0111'
$String[6] = 'NONE_2_All.t__\amm.rfw_______________0551_21'
$String[7] = 'NONE_2_All.t__\fem.bb__back_________0554'

For $Increment = 0 To UBound($String) - 1
    ConsoleWrite(StringRegExpReplace($String[$Increment], '(\_+\d+(?:\_+\d+)*)$', '-------' & StringFormat('%04s', $Increment + 1)) & @CRLF)
Next

 

When the words fail... music speaks.

Link to comment
Share on other sites

  • Developers
56 minutes ago, Zuhair said:

I have already tried my best to understand StringRegExpReplace() but it is quite difficult.

Well, post what you have that isn't working, and I am sure you are likely getting help unless you are expecting somebody to "cough up" the code like @Andreik already has done.  The advantages is that you might understand the end result code when you put in some effort first. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Posted (edited)

Here is my code that is working fine.  @Andreik code was of great help. Thank you.      

$i = InputBox("Starting Number", "Enter the starting number", "", "")
        $e = InputBox("Total lines", "How many lines?", "", "")
        $var = 0
    Do

        ClipPut('')                          ; Clear the clipboard
        $Nmbr = StringRight("000" & $i, 4)        
        MouseClick('', 450, $VP)
        Sleep(600)        
        MouseClick('', 450, $VP)
        Sleep(600)
        Send('^c')
        Sleep(100)
        $sData = ClipGet()        ; Get the copied text from the clipboard
        Sleep(50)
        $sData = StringRegExpReplace($sData, '(_+\d+(?:_+\d+)*)$', '______' & $Nmbr)    ; Replace the pattern at the end of the string
        ClipPut($sData)                ; Put the modified string back into the clipboard
        Sleep(50)
        Send('^v')
        Send('{ENTER}')
        $i += 1
        $var += 1
        $VP += 17
        If $VP > 900 Then
            Send('{PGDN 2}')
            $VP = 130
        EndIf
    Until $var >= $e

 

Edited by Zuhair
Link to comment
Share on other sites

6 hours ago, Zuhair said:

I have already tried my best to understand StringRegExpReplace() but it is quite difficult.

You have more info about regex in help file or you can use one of many online regex editors and debuggers. Basically what the pattern from previous post does is this:

\_+
matches the character _  anything between one and unlimited times, as many times as possible
 
 
\d+
matches a digit (equivalent to [0-9]) anything between one and unlimited times, as many times as possible
 
(?:\_+\d+)* This is a non-capturing group that match patterns like those explained above between zero and unlimited times, as many times as possible
 
$ asserts position at the end of a line

When the words fail... music speaks.

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...