Page 1 of 1

Find multiple matches and place all at multiple location instance

Posted: 26 Nov 2022, 06:08
by Sweetins
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.

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

Posted: 26 Nov 2022, 06:40
by mikeyww
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!

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

Posted: 26 Nov 2022, 06:53
by Sweetins
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

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

Posted: 28 Nov 2022, 02:24
by Sweetins
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