Another RegEx Question Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Another RegEx Question

Post by carno » 26 Jan 2022, 05:35

I try to combine the following 2 Positive Lookahead searches into one line of code. Is this possible? In the following clip, char p must be followed by er (as in percent) AND ar (as in partially or particularly):

Code: Select all

Clip =
(
The other difference is that trans fatty acids (often called trans fat) are formed in the hydrogenation reactor, and may amount to as much as 40 percent by weight of a partially hydrogenated oil. Hydrogenated oils, particularly partially hydrogenated oils with their higher amounts of trans fatty acids, are increasingly thought to be unhealthy.
)
RegExMatch(Clip, "p(?=er)", Match1)
If ! ( Match1 = "" )
{
RegExMatch(Clip, "p(?=ar)", Match2)
}

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Another RegEx Question

Post by teadrinker » 26 Jan 2022, 06:04

Can be par before per? What do you want to get as a result?

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Another RegEx Question

Post by mikeyww » 26 Jan 2022, 06:06

Code: Select all

regex = (per.*par|par.*per)
MsgBox % RegExMatch(Clip                     , regex, Match)
MsgBox % RegExMatch("s sperthisparthat"      , regex, Match)
MsgBox % RegExMatch("...xpar and noeper cent", regex, Match)
MsgBox % RegExMatch("...xoar and noeper cent", regex, Match)
MsgBox % RegExMatch("...xpar and noepr cent" , regex, Match)
image220126-0608-001.png
Output
image220126-0608-001.png (5.53 KiB) Viewed 741 times

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Another RegEx Question

Post by carno » 26 Jan 2022, 06:15

teadrinker wrote:
26 Jan 2022, 06:04
Can be par before per? What do you want to get as a result?
In this case order does not matter. Either way only both conditions must be met.

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Another RegEx Question

Post by carno » 26 Jan 2022, 06:25

mikeyww wrote:
26 Jan 2022, 06:06

Code: Select all

regex = (per.*par|par.*per)
MsgBox % RegExMatch(Clip                     , regex, Match)
MsgBox % RegExMatch("s sperthisparthat"      , regex, Match)
MsgBox % RegExMatch("...xpar and noeper cent", regex, Match)
MsgBox % RegExMatch("...xoar and noeper cent", regex, Match)
MsgBox % RegExMatch("...xpar and noepr cent" , regex, Match)

image220126-0608-001.png
Thanks for the code. My question is specifically if 2 positive lookahead searches can be combined in one line (AND) like the following (although this works only for OR):

Code: Select all

Clip =
(
The other difference is that trans fatty acids (often called trans fat) are formed in the hydrogenation reactor, and may amount to as much as 40 percent by weight of a partially hydrogenated oil. Hydrogenated oils, especially partially hydrogenated oils with their higher amounts of trans fatty acids, are increasingly thought to be unhealthy.
)
RegExMatch(Clip, "p(?=er)|p(?=ar)", Match1)

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Another RegEx Question  Topic is solved

Post by teadrinker » 26 Jan 2022, 06:49

A couple of options:

Code: Select all

RegExMatch(Clip, "p(?=(ar.*per|er.*par))")

Code: Select all

RegExMatch(Clip, "(p(?=er))?.*p(?=ar)(?(1)|.*(?1))")

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Another RegEx Question

Post by carno » 26 Jan 2022, 07:27

teadrinker wrote:
26 Jan 2022, 06:49
A couple of options:

Code: Select all

RegExMatch(Clip, "p(?=(ar.*per|er.*par))")

Code: Select all

RegExMatch(Clip, "(p(?=er))?.*p(?=ar)(?(1)|.*(?1))")
Thanks! Both work great. :)

Post Reply

Return to “Ask for Help (v1)”