Page 1 of 1

Another RegEx Question

Posted: 26 Jan 2022, 05:35
by carno
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)
}

Re: Another RegEx Question

Posted: 26 Jan 2022, 06:04
by teadrinker
Can be par before per? What do you want to get as a result?

Re: Another RegEx Question

Posted: 26 Jan 2022, 06:06
by mikeyww

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 782 times

Re: Another RegEx Question

Posted: 26 Jan 2022, 06:15
by carno
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.

Re: Another RegEx Question

Posted: 26 Jan 2022, 06:25
by carno
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)

Re: Another RegEx Question  Topic is solved

Posted: 26 Jan 2022, 06:49
by teadrinker
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))")

Re: Another RegEx Question

Posted: 26 Jan 2022, 07:27
by carno
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. :)