Simplify these RegEx !? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Simplify these RegEx !?

21 Nov 2016, 07:51

Hi,

I have this code

Code: Select all

newLECTURE := RegExReplace(clipboard,"s).*Look Down:")
newLECTURE := RegExReplace(newLECTURE,"\Q******************************************************************************\E")

;remove whitespaces
newLECTURE := RegExReplace(Trim(newLECTURE,"`n"),"\v+","`n")
;remove blank lines
newLECTURE := RegExReplace(newLECTURE, "`am)^\s+|\h+$|\R\z")

loop, parse, newLECTURE, `n, `r
	LineNumber = %A_Index%

if (LineNumber = 1)
	DEST := newLECTURE
So In my clipboard let's say that I have :

Code: Select all

line1
line2
line3
Look Down:
ResultLine
******************************************************************************
EmptyLine
So My result is ResultLine with these RegEx.

If you prefer I would like to have in DEST the ResultLine if it's the only line beetween Look Down: and ******************************************************************************

Any idea how to do this simpler ?

P.S.: I hope that I was clear enough ;)
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

21 Nov 2016, 08:09

I can also do something like this:

Code: Select all

StringReplace, Clipboard, Clipboard, `n, `n, UseErrorLevel
A := ErrorLevel + 1
B := A-3
C := B+1

Loop, Parse, Clipboard, `n, `r
{
	If A_Index in %B%
	{
		OutPut := A_LoopField
		Break
	}
}
If (InStr(OutPut, "avertir")){
	Loop, Parse, Clipboard, `n, `r
	{
		If A_Index in %C%
		{
			OutPut := A_LoopField
			Break
		}
	}
}
msgbox % OutPut
But not to 'optimal' I think !
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Simplify these RegEx !?

21 Nov 2016, 08:19

From what I understood, you only want to get the contents of the line in between Look Down: and 78 asterisk and it must be a single line.

Code: Select all

;MsgBox % StrLen("******************************************************************************")

text=
(
line1
line2
line3
Look Down:
ResultLine
******************************************************************************
)

RegExMatch(text,"(?<=Look Down:(?:\n|\r)).*?(?=\R\*{78})",m)
MsgBox % m
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

22 Nov 2016, 07:44

Thanks ahcahc
But it's working half.
If I have this :

Code: Select all

text=
(
line1
line2
line3
Look Down:
ResultLine
ResultLine2
ResultLine3
******************************************************************************
)

RegExMatch(text,"(?<=Look Down:(?:\n|\r)).*?(?=\R\*{78})",m)
MsgBox % m
I have the 3 ResultLines !

But I can use this RegEx for at least having these lines and after that count them.
What do you think?
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Simplify these RegEx !?  Topic is solved

22 Nov 2016, 08:16

I don't quite get what you wanted to say. But my answer was missing the `a) at the beginning of the regex to make it work the way I intended it to.

Code: Select all

text=
(
line1
line2
line3
Look Down:
ResultLine
ResultLine2
ResultLine3
******************************************************************************
)

RegExMatch(text,"`a)(?<=Look Down:(?:\n|\r)).*?(?=\R\*{78})",m)
MsgBox % m	;should be blank
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

22 Nov 2016, 13:29

Yes, this is it :dance:
With the `a) it's working like expected.

Thanks again ahcahc for your helpful answer.
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 09:06

@ahcahc
One more question please.
My real string for search is not Look Down: but * Contact(s) :
I've tried but I haven't success to change your regex for working with my real string (maybe because of the * at the beginning).

Can you please help me again once more :oops:
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Simplify these RegEx !?

23 Nov 2016, 09:45

You need to escape the *. Try \* Contact(s) :
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 10:25

Thanks, I needed also to escape the parentheses.
I don't know why but this is working with the text= but not with a fileread, text, %file%
Again thanks. I will do what I can to understand why it's not working with the fileread
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 10:25

Oups, didn't saw the reply from evilC :oops:
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Simplify these RegEx !?

23 Nov 2016, 10:36

I suspect that your text file ends in a newline, whereas the string set in the source code does not.
Either that or the line breaks in the text file are `r`n (CR+LF), whereas the line breaks in the source code are just `n
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 10:47

Well it's the double combo. I have a newline at the end and the line breaks are CR+LF
I've try to change with (?:\R)).*?(?=\R\*{78}\R\) but same.
I know I'm not too good in RegEx :oops:
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Simplify these RegEx !?

23 Nov 2016, 10:53

ozzii wrote:Thanks, I needed also to escape the parentheses.
I don't know why but this is working with the text= but not with a fileread, text, %file%
Again thanks. I will do what I can to understand why it's not working with the fileread
use "*t" option while reading the file (translate any "`r`n" to "`n")

FileRead, OutputVar, *t C:\My Documents\My File.txt
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Simplify these RegEx !?

23 Nov 2016, 10:54

Windows style linefeeds are \r\n
*nix style linefeeds are \n

Therefore, if you wish to support both, you should match on zero or one \r characters plus one \n character

Which, off the top of my head, is \r?\n
ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 11:05

@evilC
I've put these are not working (?:\r?\n)).*?(?=\r?\n\*{78}\r?\n) and (?:\r?\n)).*?(?=\r?\n\*{78})

@V for Vendetta
I've done the *t
this (?:\n|\r)).*?(?=\R\*{78}) doesn't work
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Simplify these RegEx !?

23 Nov 2016, 11:22

(?:\r?\n)) Unmatched parentheses? (One optional opening, two closing)
Plus the opening parenthesis is a capture group, but it has a ? following it? Not sure what this would do.

That does not seem to be anything to do with reading from a text file though.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

Re: Simplify these RegEx !?

23 Nov 2016, 11:25

ozzii wrote: @V for Vendetta
I've done the *t
this (?:\n|\r)).*?(?=\R\*{78}) doesn't work
use "loop, parse" instead "RegEx", much easier and faster

example:

Code: Select all

text=
(
line1
line2
line3
Look Down:
ResultLine
ResultLine2
ResultLine3
******************************************************************************
)

loop, parse, text, `n
{
	if A_LoopField = Look Down:
	{
	TextCreation = 1

	continue	;Skip lines below and start a new iteration and begins a new one
		;"a_index" count will be continued
	}

if A_LoopField = ******************************************************************************
TextCreation = 0

	if TextCreation = 1
	{
	ResultText .= A_LoopField "`r`n"
	TotalResultFound++
	}
}

msgbox, %TotalResultFound% `n`n%ResultText%

ozzii
Posts: 375
Joined: 30 Oct 2013, 06:04

Re: Simplify these RegEx !?

23 Nov 2016, 11:36

@V for Vendetta
This is what I use right now (kind a).
But I thought that a 'beautiful' regex is better.

With your code I have all the lines beetween Look Down: and *****
But I need to have the result ONLY if there is just one line

Actually I use this :

Code: Select all

StringReplace, LECTURE, LECTURE, `n, `n, UseErrorLevel
B := ErrorLevel - 2
C := B+1

Loop, Parse, LECTURE, `n, `r
{
	If A_Index in %B%
	{
		If (InStr(LECTURE, " a avertir :")){
			Loop, Parse, LECTURE, `n, `r
			{
				If A_Index in %C%
				{
					DESTINATAIRE := A_LoopField
					Break
				}
			}
		}
	}
}
If I have just one line to take, the search a avertir : is always on the fourth line from the end.
So if I find it, I take the 3rd line from the end.

But maybe something for having a better 2nd loop ?
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Simplify these RegEx !?

23 Nov 2016, 12:04

try RegExMatch(text,"`a)\* Contact\(s\) :.*?(\r\n|\n|\r)\K.*?(?=(\r\n|\n|\r)\*{78})",m).
Last edited by ahcahc on 23 Nov 2016, 12:11, edited 1 time in total.

Return to “Ask For Help”

Who is online

Users browsing this forum: alexooo, ashiruk, Bing [Bot], c7aesa7r, Google [Bot], ilical, mikeyww, Syzygy, TheProdigyC2, zoinho and 57 guests