RegExMatch help :) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

RegExMatch help :)

13 May 2021, 07:41

How to get this, using RegExMatch

Example text 1
Example text 2
Example text 3


Code: Select all

Text=
(
***********1.2.3************

Example text 1
Example text 2 
Example text 3 

***********3.2.1************
)
RegExMatch ?
My questions are baffling! :crazy:
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: RegExMatch help :)

13 May 2021, 08:21

Code: Select all

Clipboard := "", Clipboard := Trim(RegExReplace(Text, "m`a)(^\W.*?(\v+|\Z)| +$)"), "`n")
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else MsgBox, 64, Result, %Clipboard%
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 08:36

mikeyww wrote:
13 May 2021, 08:21

Code: Select all

Clipboard := "", Clipboard := Trim(RegExReplace(Text, "m`a)(^\W.*?(\v+|\Z)| +$)"), "`n")
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else MsgBox, 64, Result, %Clipboard%
Yes its works , but not quite right, if i have this text

Code: Select all

Text=
(
***********1.2.3************

Example text 1
Example text 2 
Example text 3 

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
My questions are baffling! :crazy:
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: RegExMatch help :)

13 May 2021, 08:45

We have the "It works, but what if...." situation! It will help if you provide all possible examples of your text formats, because the script is written to work with whatever you provide.
teadrinker
Posts: 4295
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExMatch help :)

13 May 2021, 08:47

Perhaps this one is correct:

Code: Select all

Text=
(
***********1.2.3************

Example text 1
Example text 2 
Example text 3 

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
RegExMatch(Text, "`am)(^Example text \d+$\R?)+", m)
MsgBox, % m
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 08:52

teadrinker wrote:
13 May 2021, 08:47
Perhaps this one is correct:

Code: Select all

Text=
(
***********1.2.3************

Example text 1
Example text 2 
Example text 3 

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
RegExMatch(Text, "`am)(^Example text \d+$\R?)+", m)
MsgBox, % m
Not correct, beacuse Example text = unknown value, this will maybe anything text

I mean this

Code: Select all

Text=
(
***********1.2.3************

asddqwlkajsfdlksaa
eqwijfjzx,mnsdapo
23190-8gdosisahdd

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
My questions are baffling! :crazy:
User avatar
boiler
Posts: 16706
Joined: 21 Dec 2014, 02:44

Re: RegExMatch help :)

13 May 2021, 08:57

@djuga - Your instructions are still incomplete. Do you want to extract the text only after the first section that is marked by a header and before the next section that is marked by a header, or must the header contain 1.2.3? It would again be possible to meet your exact latest example and then you show another example where it doesn't work by your new definition. Also, will there always be a blank line after the header and before the next one?
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:01

boiler wrote:
13 May 2021, 08:57
@djuga - Your instructions are still incomplete. Do you want to extract the text only after the first section that is marked by a header and before the next section that is marked by a header, or must the header contain 1.2.3? It would again be possible to meet your exact latest example and then you show another example where it doesn't work by your new definition. Also, will there always be a blank line after the header and before the next one?
Value 1.2.3 maybe whatever, need to get the first section text inside ***RandomValue*** ->some text<- ***RandomValue***
My questions are baffling! :crazy:
User avatar
boiler
Posts: 16706
Joined: 21 Dec 2014, 02:44

Re: RegExMatch help :)  Topic is solved

13 May 2021, 09:10

Code: Select all

Text=
(
***********1.2.3************

asddqwlkajsfdlksaa
eqwijfjzx,mnsdapo
23190-8gdosisahdd

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
RegExMatch(Text, "^\*+\d+\.\d+\.\d+\*+\v*\K.*?(?=\v+\*+\d+\.\d+\.\d+\*+)", m)
MsgBox, % m
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:11

boiler wrote:
13 May 2021, 09:10

Code: Select all

Text=
(
***********1.2.3************

asddqwlkajsfdlksaa
eqwijfjzx,mnsdapo
23190-8gdosisahdd

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
RegExMatch(Text, "^\*+\d+\.\d+\.\d+\*+\v*\K.*?(?=\v+\*+\d+\.\d+\.\d+\*+)", m)
MsgBox, % m
My big thx! Thats it :dance:
My questions are baffling! :crazy:
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: RegExMatch help :)

13 May 2021, 09:13

Here's mine-- perhaps not as accurate as @boiler's.

Code: Select all

RegExMatch(Text, "\n\n\K.+?(?=\n\n)", new)
Send {Text}%new%
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:17

mikeyww wrote:
13 May 2021, 09:13
Here's mine-- perhaps not as accurate as @boiler's.

Code: Select all

RegExMatch(Text, "\n\n\K.+?(?=\n\n)", new)
Send {Text}%new%
Yes this also works :D

But idk why this doenst work from fileread
FileRead, Text, test.txt
Last edited by djuga on 13 May 2021, 09:18, edited 1 time in total.
My questions are baffling! :crazy:
sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: RegExMatch help :)

13 May 2021, 09:18

Maybe something like this one?

Code: Select all

Text=
(
***********1.2.3************

Example text 1
Example text 2 
Example text 3 

***********3.2.1************

Bla, bla
Bla, bla

***********4.5.6************

Example text 1
Example text 2 
Example text 3 

***********1.2.3************

)

Templ  := "(\*+?[^*]+\*+?\R+)"
Templ2 := "\K(.*?)"
while pos := RegExMatch(Text, "s)" Templ Templ2 Templ, m, A_Index=1?1:pos+StrLen(m))
	MsgBox, % m2
User avatar
boiler
Posts: 16706
Joined: 21 Dec 2014, 02:44

Re: RegExMatch help :)

13 May 2021, 09:19

I thought about the approach with just checking the newlines but was thinking you might then say it doesn't work with this:

Code: Select all

Text=
(
***********1.2.3************

asddqwlkajsfdlksaa
eqwijfjzx,mnsdapo

23190-8gdosisahdd

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:25

boiler wrote:
13 May 2021, 09:19
I thought about the approach with just checking the newlines but was thinking you might then say it doesn't work with this:

Code: Select all

Text=
(
***********1.2.3************

asddqwlkajsfdlksaa
eqwijfjzx,mnsdapo

23190-8gdosisahdd

***********3.2.1************

This text doesnt needed 4
This text doesnt needed 5
This text doesnt needed 6

***********4.5.6************
)
Your way is perfect, but how about fileread ? :)
My questions are baffling! :crazy:
User avatar
boiler
Posts: 16706
Joined: 21 Dec 2014, 02:44

Re: RegExMatch help :)

13 May 2021, 09:29

Instead of assigning the text to Text, replace it with:

Code: Select all

FileRead, Text, my file.txt
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:34

boiler wrote:
13 May 2021, 09:29
Instead of assigning the text to Text, replace it with:

Code: Select all

FileRead, Text, my file.txt
I know it :lol: , but its doesnt work if you read data from file
My questions are baffling! :crazy:
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: RegExMatch help :)

13 May 2021, 09:34

Code: Select all

FileRead, Text, %A_ScriptDir%\test.txt
RegExMatch(Text, "s)\*\v+\K.+?(?=\v+\*)", new)
SendInput {Text}%new%
djuga
Posts: 88
Joined: 28 Jul 2019, 08:16

Re: RegExMatch help :)

13 May 2021, 09:37

mikeyww wrote:
13 May 2021, 09:34

Code: Select all

FileRead, Text, %A_ScriptDir%\test.txt
RegExMatch(Text, "s)\*\v+\K.+?(?=\v+\*)", new)
SendInput {Text}%new%
This works
RegExMatch(Text, "s)\*\v+\K.+?(?=\v+\*)", new)

This not
RegExMatch(Text, "^\*+\d+\.\d+\.\d+\*+\v*\K.*?(?=\v+\*+\d+\.\d+\.\d+\*+)", new)
My questions are baffling! :crazy:
User avatar
boiler
Posts: 16706
Joined: 21 Dec 2014, 02:44

Re: RegExMatch help :)

13 May 2021, 09:55

djuga wrote: This not
RegExMatch(Text, "^\*+\d+\.\d+\.\d+\*+\v*\K.*?(?=\v+\*+\d+\.\d+\.\d+\*+)", new)
Just put s) in front:

Code: Select all

RegExMatch(Text, "s)^\*+\d+\.\d+\.\d+\*+\v*\K.*?(?=\v+\*+\d+\.\d+\.\d+\*+)", new)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: inseption86, peter_ahk, william_ahk and 172 guests