RegEx Pattern Help

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

RegEx Pattern Help

Post by PepsiHog » Wed Apr 24, 2024 8:20 pm

Hello Everyone,

I have this:

Code: Select all

let>ListOne=2;7;49;4;16;19;42;38;39;1;8;17;13;12;39;51;9;20;29;35;36;37;10;50;

let>Pattern=\b(49|38|17);(13|26|39|52)
// Pay no mind to the following RegEx. It is one of many tests to get the desired result.
RegEx>Pattern,ListOne,0,Match,DLnom,1,$1,Test

Ok. I need to find a number. Such as 13. Then I need to check the number just before 13. If that number is one of the numbers listed, then keep that number but exclude the 13.

So I search for 49 or 38 or 17 followed by 13 or 26 or 39 or 52. If found, I want to keep the first number and exclude the second number. So, in this example, I want to exclude 13 or 26 or 39 or 52. So that the result is only 49 or 38 or 17. So if it finds 38;39 the result is 38.

There are multiple combinations that are possible. It could be 38;39 or 49;13 or 17;52. Any combination of the first set of numbers with the second set of numbers, with only one of each. The number can be one digit or two digits. Note that all numbers here are examples.

There may also be more than one result. So it may find both 38;39 and 49;13 and maybe even 17;52. In which I want the first number of each set(38;49;17).

If you can help, your help will be greatly appreciated.

Thanks,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

robsmith
Newbie
Posts: 17
Joined: Mon Oct 30, 2023 2:12 pm
Location: USA

Re: RegEx Pattern Help

Post by robsmith » Fri Apr 26, 2024 7:40 pm

It sounds like you want a positive lookahead group.

Code: Select all

Let>string=2;7;49;4;16;19;42;38;39;1;8;17;13;12;39;51;9;20;29;35;36;37;10;50;
Let>pattern=\d+(?=;13|;29)
Regex>pattern,string,0,matches,num_matches,0
\d+ means 1 one more consecutive digits.
(?=;13) is the positive lookahead, means, only match \d+ when it comes before ;13
The pipe | is alternation, so (?=;13|;29) means match if it comes before ;13 OR ;29

This should get you close. I suggest you use a regex builder like: https://regexr.com/ or https://regex101.com/ anytime you are using RegEx> as this will make you smarter, faster.

The learning curve is frustrating at first, but well worth it, as you can use regular expressions in most every scripting language.

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx Pattern Help

Post by hagchr » Sat Apr 27, 2024 4:53 am

To develop it further:
(Here you look for a position that is followed by a pair of numbers. If you find a position, then take the first number)

Code: Select all

Let>string=2;7;49;4;16;19;42;38;39;1;8;17;13;12;39;51;9;20;29;35;36;37;10;50;

Let>pattern=(?=(49|38|17);(13|26|39|52))\d+
Regex>pattern,string,0,matches,num_matches,0

// Alt:

Let>groupA=(49|38|17)
Let>groupB=(13|26|39|52)
Let>pattern=(?=%groupA%;%groupB%)\d+
Regex>pattern,string,0,matches,num_matches,0

Let>res=
Let>k=0
While>k<num_matches
  Add>k,1
  Let>tmp=matches_%k%
  Let>res=%res%%tmp%%CRLF%
EndWhile

MDL>res

User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: RegEx Pattern Help

Post by PepsiHog » Sun May 05, 2024 5:35 pm

Thank you robsmith.

Thank you Hagchr.

I couldn't figure it out. But thanks to both of you, now I know for the furture.

Ya'll are awesome!,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts