Consume a result

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

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

Consume a result

Post by PepsiHog » Tue Dec 22, 2020 8:17 pm

Greetings,
I've done this before, but it's been so long, I forget how it's done.

Code: Select all

let>Numbers=23:36;41:33;10:49;41:33;17:30;24:37;
RegEx>\b(\d\d?)\b(?=.*\b\1\b),Numbers,0,matchA,nomA,0
In the above script, the number 33 repeats. I am trying to remember how to get RegEx to consume one of the repeated numbers. The colon can be replaced by a semicolon. Or not, it makes no difference.

I know the above pattern is just a basic lookahead. I'm trying to remember. I'm thinking it may have been a lookbehind.

Anyone know the answer? Sure would appreciate any help.

Thanks,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Consume a result

Post by PepsiHog » Tue Dec 22, 2020 10:09 pm

I got it. Never mind. It was in the matches, just had to bring them together.

Don't you just hate problems that hide in plain sight?

PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Consume a result

Post by PepsiHog » Tue Dec 22, 2020 10:59 pm

Howdy,
So I just started playing around with RegEx. I didn't expect to solve anything more, I was just playing. I have these screen captures of these cheat sheets I captured from online. I was just looking at a cheat sheet and saw this part about naming a group. I've seen this before, but I never thought it would be useful.

Boy! Was I wrong! Naming a group gives you access to the full result of said group.

Here is the script:

Code: Select all

let>Numbers=23:36;41:33;10:49;41:33;17:30;24:37;

RegEx>\b(?P<Dove>\d\d?(;|:))\b(?=.*\b\1\b),Numbers,0,matchA,nomA,1,$1,Test
//$1=23:36;41:33;10:49;41:33;17:30;24:37; 

RegEx>\b(?P<Dove>\d\d?(;|:))\b(?=.*\b\1\b),Numbers,0,matchA,nomA,1,$Dove,Test
//$Dove=23:36;10:49;41:33;17:30;24:37;

You see the first RegEx and then the result under it. The first one I used $1 for the result. That just returned the unmodified string.

The second RegEx I used $Dove(it's name) and the result is the full result. I don't need to piece together each match. That's very useful and time saving. And less code lines.

I'm very excited about this discovery and I thought I just had to share it.

PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Consume a result

Post by hagchr » Fri Dec 25, 2020 3:44 pm

Hi! Nice to see someone interested in Regex - your best friend and your worst enemy.

If I understand it correctly you just want to remove duplicates. You can do this by simply taking the matched numbers and replace them with nothing, ie leave the replacement string empty. Your example without the named group:

Code: Select all

let>Numbers=23:36;41:33;10:49;41:33;17:30;24:37;
RegEx>\b(\d\d?(;|:))\b(?=.*\b\1\b),Numbers,0,matchA,nomA,1,,Test
I think this is what you in effect are doing in your second example (there is an issue with how $Dove is used).

Look at these examples ($Dove is empty):

Code: Select all

Let>Numbers=10;20;30;40:60
RegEx>(?P<Dove>(10|60)),Numbers,0,matchA,nomA,1,"$Dove",Test
MDL>Test

Let>Numbers=10;20;30;40:60
RegEx>(?P<Dove>(10|60)),Numbers,0,matchA,nomA,1,"$<Dove>",Test
MDL>Test
Challenges for you:
- If you eg add the number 23: at the end, it will not work.
- If the same number is followed by ; in one instance and : in another it will not work.

How do you adjust the pattern so that it also works in those cases?

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

Re: Consume a result

Post by PepsiHog » Sat Dec 26, 2020 12:04 am

Hey hagchr,
Merry Christmas! And now... on with the show....

Actually, no, you do not have it right. I want the dups. The dups indicate a positive match between two strings. I am looking to see if a result happened twice. Working on a macro that deals with numbers, and $Dove was an awesome find for me. Saves me sooooo much work.

But thanks for responding. I always appreciate your time and thoughtful help. I know you are also a guru of RegEx. When I need RegEx help I always hope the gurus, such as yourself, see my posts. (and reply)

Challenges. Yea, you're right about that. But between my posting and your awesome insight, I succeeded in making it work fully in the way it was needed. Just as you pointed out. Some minor, almost unnoticeable, change made it work just right. That's how it is with RegEx, one little thing, you'd think would make no difference, makes all the difference in the world.

I also learned that as much as I learned, when I needed the same RegEx again, it was going to be the same fight to get it just right,.......so I just copied it. :lol:

PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 511
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Re: Consume a result

Post by PepsiHog » Sat Dec 26, 2020 12:21 am

I sound like I'm asking for something different in the initial post. I wanted to exclude them, with the result being what I excluded. Like I said, I was pretty sure I had done it before.

But sorry if I was misleading or unclear. Frankly, it wasn't clear in my head either.

I thought you may like to see the final RegEx.

Code: Select all

RegEx>\b(?P<Base>\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,$Base,Results
Thanks so much,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Consume a result

Post by hagchr » Sat Dec 26, 2020 10:06 am

Hi again,

Great that it works and all the best for 2021! The story could have ended here if it wasn't for curious-me. Looking at your final pattern I see you first match/find all numbers that do not have a duplicate, as well as all extra duplicates (smart!). Then you replace the result with $Base giving you the string Results. The problem is, I think $Base is empty, which works here because you want to remove all matched numbers. So in effect you replace what you do not want with nothing. You can test by removing $Base as the replacement string. Do you still get the same result?

Code: Select all

RegEx>\b(?P<Base>\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,,Results
If you want to refer to the named group in the replacement string I think you need to use $<Base> instead of just $Base. However, in this example that would give you the wrong result.

In case I misunderstood your problem again, then pls disregard.

All the best for 2021!

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

Re: Consume a result

Post by PepsiHog » Sat Dec 26, 2020 5:31 pm

Hello hagchr,
I'm not sure. I think you may be on a level with RegEx that I haven't yet achieved.

How can $base be empty? $Base is the result that I'm asking for. If I used $1, that would ask for the result of the first group in the pattern.

I don't mind your inquiries. I might learn something that I don't know. So please don't give up. When it comes to talking about programming, I'm always interested.

It sounds like you might believe I'm doing it the hard way(and I just might be). How would you go about this?

I have a set of numbers in a string. All I'm interested in are the numbers that repeat. But I don't need the repeated number. If 24 repeats, then in my result all I want is 24, not 24 24.

Here is a sample macro for you to check out.

Code: Select all

let>Using=24:36
let>MyList=50;8;24;16;2;5;51;

Separate>Using,:,Num

let>CompareStrings=%Num_1%;%Num_2%;%MyList%

StringReplace>CompareStrings,:,;,CompareStrings

RegEx>\b(?P<Bob>\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,$Bob,Results

Separate>Results,;,NumStr


mdl>%NumStr_1%
Please share. I'm all ears.

A Happy New Year to you as well,
PepsiHog
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Consume a result

Post by hagchr » Sat Dec 26, 2020 7:54 pm

Hi again,

In your previous example, $Base includes all numbers that do not have a duplicate, as well as all duplicates. So really you want everything except $Base. I think the correct way to refer to a named group in the replacement field would be $<Base> not $Base, so I think $Base is wrong syntax and that is why you get nothing. (Which is really what you want in this case).

In your latest example, just try it without replacing with $Bob and you should see the same answer.

Code: Select all

let>Using=24:36
let>MyList=50;8;24;16;2;5;51;
Separate>Using,:,Num
let>CompareStrings=%Num_1%;%Num_2%;%MyList%
StringReplace>CompareStrings,:,;,CompareStrings
RegEx>\b(?P<Bob>\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,,Results
Separate>Results,;,NumStr
mdl>%NumStr_1%
0: COMPARESTRINGS=24;36;50;8;24;16;2;5;51;
$Bob contains all numbers except the first 24 (all other numbers do not have a duplicate to the right, your look-ahead criteria)
Replace $Bob with nothing in CompareStrings and you are left with only number 24

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

Re: Consume a result

Post by hagchr » Sat Dec 26, 2020 7:59 pm

If I adjust your script and add the number 2 to Using then...

Code: Select all

let>Using=24:36:2:
let>MyList=50;8;24;16;2;5;51;

Let>CompareStrings=%Using%%MyList%
StringReplace>CompareStrings,:,;,CompareStrings
RegEx>\b(\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,,Results
MDL>Results

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

Re: Consume a result

Post by PepsiHog » Sat Dec 26, 2020 10:11 pm

hagchr,
Ok. I see what you're saying. I thought if the result was left out you'd have no result. So what you're telling me is naming is unnecessary.

Ok. See you just learnited me. (That's an intentional joke.)

I didn't know leaving out a result still produced a result. Interesting.

Thanks for your patience. But now I'm left with what I originally thought. Why would you ever need to name?

Just sheer curiosity, you called it a lookahead, you actually know it's a lookbehind, right?

Thanks again,
PepsiHog

[edit] - When you presented me the RegEx with no result, because of what I didn't know is why I didn't try it and it escaped me. Just goes to show, you should always try the example. Sorry for that. I'm just a difficult student.
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2021) 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: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Consume a result

Post by hagchr » Sun Dec 27, 2020 10:07 am

Hi again,

Looking at your regex:

Code: Select all

RegEx>\b(\d\d?);(?!.*\b\1\b),CompareStrings,0,match,nom,1,,Results
0: COMPARESTRINGS=24;36;50;8;24;16;2;5;51;

You go through your string left to right and try to find/match two digits and a ; and then look ahead to ensure that there are no duplicates to the right. When you look ahead after the first 24; you see there is a duplicate => NO MATCH, then after 36; you look ahead and see there are no duplicates => MATCH etc...

Technically since you have (?!...) you can call it a negative lookahead.

You make me interested in looking more into named groups. I guess one advantage is if you have complex patterns with several () - then capture groups / back references / replacements / will get messed up if you just change one ( or ) somewhere. If you have named a group then it is stable whatever () you have/change elsewhere.

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