RegEx 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 Help

Post by PepsiHog » Wed Apr 26, 2023 11:21 pm

Hello Everyone,
I am trying to advance my skill level in RegEx. I started playing using RegEx with some random numbers. Just seeing what I can come up with to challenge myself.

I want to extract a set of numbers from the result, but have been unsuccessful. Here is the macro as it is now.

Code: Select all

LabelToVar>NumberTest,hnTest

// List of numbers separated by the bar(the 'or' in RegEx).
let>NumOrList=22|28|20|19|30|33|37

let>Pattern1=((%NumOrList%):(\d\d?);(\d\d?:\d\d?;))|((\d\d?):(\1);(\d\d?:\d\d?;))

RegEx>%Pattern1%,hnTest,0,match,nomb,0

mdl>%nomb%  (%match_1%) (%match_2%) (%match_3%)

/*
NumberTest:
13:52;25:38;11:50;23:36;9:48;21:34;7:46;19:32;5:44;17:30;3:42;15:28;0:0;26:39;12:51;24:37;10:49;22:35;8:47;20:33;6:45;18:31;4:43;16:29;2:41;0:0;
*/


// Result
//3  (19:32;5:44;) (22:35;8:47;) (20:33;6:45;)
// ========^^^^^=======^^^^^======^^^^^

The carrots(^) show the numbers I wanted to extract. I tried using the group number, but that doesn't work. I think it may not be working due to the pattern has 'or' arguments in it.

Of course, I am aware that I could extract the numbers through other means, but that isn't what this is about.

To clarify. I want to extract these sets of numbers.
5:44;8:47;6:45;

Ok, so.....what do you RegEx experts think? What is the solution here?

Thanks for helping,
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!

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

Re: RegEx Help

Post by hagchr » Thu Apr 27, 2023 6:25 am

Hello,

You can try:

Code: Select all

let>Pattern1=((%NumOrList%):(\d\d?);\K(\d\d?:\d\d?))|((\d\d?):(\1);(\d\d?:\d\d?))
(where \K instructs to forget what was just matched and continue.)

or since right part is not used here:

Code: Select all

let>Pattern1=((%NumOrList%):(\d\d?);\K(\d\d?:\d\d?))
or to reduce further:

Code: Select all

let>Pattern1=(%NumOrList%):\d\d?;\K\d\d?:\d\d?

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

Re: RegEx Help

Post by PepsiHog » Thu Apr 27, 2023 8:13 pm

Hello hagchr,
First let me say, thank you! Just what I needed. Awesome!

The second part flips which side of the colon the number is on. The number is one of the two(19:32). It may be looking for 19, like it is in this example, but it may be looking for 32. I have no way of knowing which it will be. The two numbers could be any of the two number sets in the data block.

The \d\d?(after the colon) is the unknown number, the one that isn't being searched for. In the second part of the pattern, the second number(\1) is the list of numbers being searched for(NumOrList). Separated by an 'or'(|). So RegEx will run through each number until it makes a match or runs out of numbers.

So RegEx is running through all the numbers in the first part of the pattern and then it runs through all the same numbers for the second part of the pattern. The difference being which side of the colon the number is on.

Now thinking about it, probably could've had it search both sides at the same time. But six to one, half a dozen to another. It works, not gonna rewrite it now.

let>Pattern1=((%NumOrList%):(\d\d?);\K(\d\d?:\d\d?))|((\d\d?):(\1);(\d\d?:\d\d?))

Blah, Blah, Blah, right? Told you that why? Beats me. But enjoy the fruits.

Thanks again,
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!

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

Re: RegEx Help

Post by PepsiHog » Fri Jun 09, 2023 5:07 pm

hagchr,
Hoping you could help me out here (again). I changed my RegEx pattern. The result contains three(3) sets of numbers. Such as: 13:52;25:38;11:50. In this example, I want only 25:38. I could just process the result, but I would prefer to have RegEx do it. I keep trying to refer to the group, but that isn't working. Probably other factors that I am unaware of. \K resets the beginning of the match. I tried to find online if I could reset the end of the match, but had no luck. Maybe you know if this is possible. Or do you know a way to get the wanted result?

Code: Select all

LabelToVar>MyData,DataStr

let>Lista=22|28|20|19|30|33|37
let>Listb=22|28|20|19|30|33|37
// Lista and Listb would differ normally, but it doesn't matter here.

 let>Pattern2=((%Listb%):(\d\d?)|(\d\d?):(%Listb%));(\d\d?:\d\d?);((%Lista%):(\d\d?)|(\d\d?):(%Lista%))
// This is what I am after ======================^^^^^^^^^

 RegEx>%Pattern2%,DataStr,0,Match,nomb,0

/*
MyData:
13:52;25:38;11:50;23:36;9:48;21:34;7:46;19:32;5:44;17:30;3:42;15:28;0:0;26:39;12:51;24:37;10:49;22:35;8:47;20:33;6:45;18:31;4:43;16:29;2:41;0:0
*/
Any user that can help is always welcome to reply as well.

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!

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

Re: RegEx Help

Post by hagchr » Sat Jun 10, 2023 6:43 am

Hi, you can eg try:

Code: Select all

let>Pattern2=(?=((%Listb%):(\d\d?)|(\d\d?):(%Listb%));(\d\d?:\d\d?);((%Lista%):(\d\d?)|(\d\d?):(%Lista%)))[^;]+;\K[^;]+
Basically what is does is start from the left and look for positions that are followed by the three pairs (where the first and third pair contain number(s) from your list). Then step through the first number and ; then use \K to forget. Finally match the next number.

I also note that in your example there are 4 numbers that are surrounded with pairs containing numbers from your list. (Before when you looked for all three pairs then you only got 2 matches since the pairs are next to each other and when matching one you kill the other).

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

Re: RegEx Help

Post by hagchr » Sat Jun 10, 2023 6:54 am

Also I note the number of brackets (), that all capture information. Not a performance problem here but could be for larger data sets. For larger problems, if you need brackets but want to turn capturing off then simply change (xxxx) to (?:xxxx).

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