Parsing file line by line regex Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahk 47
Posts: 5
Joined: 24 Jul 2017, 15:10

Parsing file line by line regex

14 Aug 2017, 08:39

Hello,

I encountered a blocking point while trying to extract a word (text) from a file.
Below is the text that is stored in the file:
  • This is just some random text.

    Write any text, but the text should be this long.

    If text exist, than it' ok.
This is my solution:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance Force
Gui, Add, Text, x10 y12, Load file.
Gui, Add, Button, x10 y30 w90 h20 gloadfile, Open File
Gui, Show, w300 h300,TEST

return
GuiClose:
  ExitApp 
loadfile:
  FileSelectFile, eFile, 3, , Open the file, Text Documents (*.txt; *.xml; *.html; *.csv)
  fileread,contents,%eFile%
  RegExReplace(contents,"\n","",totalLines) ;match the number of new line character
  totalLines++ ;always 1 short
	loop,parse,contents,`n
  {
    OutputVar := RegExMatch(A_LoopField, "(text)",var1)
    MsgBox, %OutputVar% & %var1%
  }
return
The problem is that in the online regex tester it will find 4 matches of word text, but with my code it will find only 3.
I know that the code does exactly what i tell him to do, it extracts line by line from file,and the first occurrence is stored in var1 and after that it continues the loop.
Any ideas how to make it to find all the words text?
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Parsing file line by line regex  Topic is solved

14 Aug 2017, 10:05

Hello Ahk 47.

Welcome to the AutoHotkey community forums.

By default, RegExMatch() will return the first (or leftmost) occurence of the needle (the word "text" in this case) within the haystack (the current line, in this case), which means that if you run it once per line,this line:

Code: Select all

Write any text, but the text should be this long.
Will return only the leftmost occurence of "text" and than your code will have the script immediately skip to the next line.

There are many solutions to this issue, all pertaining to the creativity of the programmer. One way that i can think of is to, instead of running one RegExMatch() by line, remove all the characters to the left of the current result and than run the next RegExMatch() in all text that is left. Also adjust some offset counters if you whant an offset relative to the current line. Example bellow:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance Force
Gui, Add, Text, x10 y12, Load file.
Gui, Add, Button, x10 y30 w90 h20 gloadfile, Open File
Gui, Show, w300 h300,TEST

return
GuiClose:
  ExitApp 
loadfile:
  FileSelectFile, eFile, 3, , Open the file, Text Documents (*.txt; *.xml; *.html; *.csv)
  fileread,contents,%eFile%
  RegExReplace(contents,"\n","",totalLines) ;match the number of new line character
  totalLines++ ;always 1 short
	loop,parse,contents,`n
  {
	Counter := 0 ; COUNTER IS ZEROED SO THAT THE RESULT IS AN OFFSET OF CURRENT LINE (ERASE ANY OFFSETS OF PAST LINES)
	CURRENT_LINE := A_LoopField
	While, OutputVar := RegExMatch(CURRENT_LINE,"(text)",var1)
	{
		Counter += OutputVar ; COUNTER IS INCREMENTED SO THAT IN A LINE WITH MULTIPLE RESULTS, THE MSGBOX OFFSETS THE CORRECT POSITION.
		MsgBox, %Counter% & %var1%
		StringTrimLeft, CURRENT_LINE, CURRENT_LINE, %OutputVar%
	}
  }
return
Once again, since this is mostly creativity, there may well be much better ways to do it, so take it just as an example. This solution also ignores lines without any results and is case sensitive.

Best wishes.
ahk 47
Posts: 5
Joined: 24 Jul 2017, 15:10

Re: Parsing file line by line regex

18 Sep 2017, 10:00

Gio wrote:Hello Ahk 47.

Once again, since this is mostly creativity, there may well be much better ways to do it, so take it just as an example. This solution also ignores lines without any results and is case sensitive.

Best wishes.
Hi Gio,
Your solution works great.
Thank you for your help!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], mikeyww, PsysimSV, USS_Sandhu and 317 guests