Find multiple matches and place all at multiple location instance Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Find multiple matches and place all at multiple location instance

Post by Sweetins » 26 Nov 2022, 06:08

Hi all, so I have this comma delimited file that I need to import but I need to do some replacements first. A sample is as shown below;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,,,,end
1 item A,content,content,,,,,,,end
2 item A,content,content,,,,,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,,,,end
1 item B,content,content,,,,,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,,,,end
2 item B 1,content,content,,,,,,,end
2 item B 2,content,content,,,,,,,end
3 item B,content,content,,,,,,,end

Each main item (eg. item A) is followed by its subitems (eg. 1 item A), which might also have sub-subitem (eg. 2 item B 1) as shown above. The objective is to perform a multiple match on the main items and place all matches at a specific location in both the main item and its subitems as shown below;
item A,<u>1 a1</u> red<u>2 a2</u> etc,content,,,,1 a1<br>2 a2<br>3 a3,,,end
1 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
2 item A,content,content,,,,1 a1<br>2 a2<br>3 a3,,,end
item B,<u>1 b1</u> yellow<u>2 b2</u> green<u>3 b3</u>etc,content,,,,1 b1<br>2 b2<br>3 b3,,,end
1 item B,content,content,,,,1 b1<br>2 b2<br>3 b3,,,end
2 item B,<u>1 2b1</u> black<u>2 2b2</u> etc,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 1,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
2 item B 2,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end
3 item B,content,content,,,,1 b1<br>2 b2<br>1 2b1<br>2 2b2<br>3 b3,,,end

The regular expression (?:^[^,]+,.*?<u>(\d{1,2} .+?)</u>[^,]*,) does match, but the challenge is how to get the multiple matches and place them at the desired location in all respective major and sub items. Can anyone help with this? Thanks already.
Last edited by Sweetins on 28 Nov 2022, 02:50, edited 4 times in total.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Find multiple matches and place all at multiple location instance

Post by mikeyww » 26 Nov 2022, 06:40

Just one idea, you could use Loop to read each line, and then build your output file or string.

https://www.autohotkey.com/docs/commands/LoopReadFile.htm

I had trouble following your pattern of 1 1. If you don't know your own pattern, your coding will be a greater challenge!

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Find multiple matches and place all at multiple location instance

Post by Sweetins » 26 Nov 2022, 06:53

mikeyww wrote:
26 Nov 2022, 06:40
I had trouble following your pattern of 1 1.
Thanks for pointing that out. The oversight is corrected

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Find multiple matches and place all at multiple location instance  Topic is solved

Post by Sweetins » 28 Nov 2022, 02:24

mikeyww wrote:
26 Nov 2022, 06:40
Just one idea, you could use Loop to read each line, and then build your output file or string.
Ended up tweaking the initial regular expression a bit to avoid too many backtracking. Not sure if its most efficient, but the following script I came up with, got the job done (with some few practical modification)! Any optimization is welcomed as well. Thanks again

Code: Select all

i:=1, Textds:=[], linen:=0, mainhr:=vList1:=""
FileRead, vList1, C:\Input.txt
loop, parse, vList1, `n,`r`n
{
    linen++
    Textds[linen] := StrReplace(A_LoopField,"<u>", "¤")
    }
linen:=0
While (Textds[i])
{
    mainhr:= RegExReplace(RegExReplace(Textds[i],"[^¤]*¤(\d{1,2} .+?)</u>(?=(?:[^,]*,){8})", "$1@"),"@[^@]+$", "@")
    if (mainhr)
    {
        linen:=i, RegExMatch(Textds[i],"^([^,]+),", mainitem)
        Loop {
                    if RegExMatch(Textds[++i],"^(\d{1,2}) [^,]+,¤", subhr)
                        mainhr:= RegExReplace(mainhr,"^((?:[^@]+@){" subhr1 "})", "$1" RegExReplace(RegExReplace(Textds[i],"[^¤]*¤(\d{1,2} .+?)</u>(?=(?:[^,]*,){8})", "$1ð"),"ð[^ð]+$", "ð"))
                    RegExMatch(Textds[i],"^\d{1,2} ([^,]+),", subitem)
                    subitem1:=RegExReplace(subitem1," \d{1,2}$", "")
                } Until (subitem1!=mainitem1)
        While (linen!=i)
        {
            mainhr:=RegExReplace(RegexReplace(mainhr, "ð|@", "<br>"),"<br>$", "")
            Textds[linen] := StrReplace(Textds[linen],"¤", "<u>")
            FileAppend, % RegexReplace(Textds[linen], "(,,,[^,\r\n]*$)", mainhr "$1") "`r`n", C:\Output.txt
            linen++
            }
        }
            else
        MsgBox, % "Error at line " i
    }
MsgBox, Done
return

Post Reply

Return to “Ask for Help (v1)”