Page 1 of 2

Simplify these RegEx !?

Posted: 21 Nov 2016, 07:51
by ozzii
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 ;)

Re: Simplify these RegEx !?

Posted: 21 Nov 2016, 08:09
by ozzii
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 !

Re: Simplify these RegEx !?

Posted: 21 Nov 2016, 08:19
by ahcahc
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

Re: Simplify these RegEx !?

Posted: 22 Nov 2016, 07:44
by ozzii
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?

Re: Simplify these RegEx !?  Topic is solved

Posted: 22 Nov 2016, 08:16
by ahcahc
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

Re: Simplify these RegEx !?

Posted: 22 Nov 2016, 13:29
by ozzii
Yes, this is it :dance:
With the `a) it's working like expected.

Thanks again ahcahc for your helpful answer.

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 09:06
by ozzii
@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:

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 09:45
by ahcahc
You need to escape the *. Try \* Contact(s) :

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 09:57
by evilC
( is also a character with special meaning in Regexes. You need to escape brackets with \ also

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:25
by ozzii
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

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:25
by ozzii
Oups, didn't saw the reply from evilC :oops:

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:36
by evilC
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

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:47
by ozzii
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:

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:53
by V for Vendetta
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

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 10:54
by evilC
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

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 11:05
by ozzii
@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

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 11:22
by evilC
(?:\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.

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 11:25
by V for Vendetta
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%


Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 11:36
by ozzii
@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 ?

Re: Simplify these RegEx !?

Posted: 23 Nov 2016, 12:04
by ahcahc
try RegExMatch(text,"`a)\* Contact\(s\) :.*?(\r\n|\n|\r)\K.*?(?=(\r\n|\n|\r)\*{78})",m).