Finish this, please.

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

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

Finish this, please.

Post by PepsiHog » Sun Feb 24, 2013 12:06 am

Hello Everyone,

Hoping someone can tell me how to do this. I have a RegEx looking for one pattern or another. But I only want the first instance to be removed, not the second. (if there even is a second)

Here's the setup.

let>Number=10
let>Data=01:02;10:22;08:32;44:52;10:22;

(I'm trying to make this as simple as I can.)

Here is the current RegEx that I have so far.

RegEx>(\d\d:%Number%)|(%Number%:\d\d);,Data,0,match,nm,1,,Data

This works fine. The only thing I'm not sure how to do is to tell it I only want the first instance to be removed.

The data is separated by simicolons. One data is 10:22.

See the number groups are always low:high. So in this case 10 is being searched for, but the macro won't know if 10 is the low or high number. So I need it to search for both possibilities.

10 being the low (%Number%:\d\d)

and

10 being the high (\d\d:%Number%).

I need it to remove the first instance and stop. It will be possible for there to be more than one instance of the same group of numbers.

They will always be low:high. It needs to remove both numbers on either side of the colon, as well as the colon itself. And the ending simicolon.

I believe all that is needed is the pattern to express first instance only.

Thanks in advance,
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
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sun Feb 24, 2013 6:59 am

PepsiHog wrote:Hoping someone can tell me how to do this. I have a RegEx looking for one pattern or another. But I only want the first instance to be removed, not the second. (if there even is a second)
Sure thing - here's how I would do that:

Code: Select all

let>Number=10
let>Data=01:02;10:22;08:32;44:52;10:22;

MDL>Data

Let>pattern=(\d\d:%Number%)|(%Number%:\d\d);(.*)
RegEx>pattern,Data,0,match,nm,1,$3,Data

MDL>Data
Edit: Mon Feb 25, 2013 - There is something wrong with the above pattern.
Read subsequent posts below to find out more... jpuziano


Note that this uses the pattern you were already using and the extra (.*) tacked on at the end just matches everything else after that... therefore this pattern will only match once no matter how many occurences of \d\d:10 or 10:\d\d there are... its only going to match the first one it comes across.

Note the $3 we replace the match with. ( round brackets ) create capture groups and we have three of them in the pattern. The everything else is stored in the 3rd capture group $3 so when we replace the match with $3 we are putting back the everything else that was matched but leaving out the chars matched by your original pattern... effectively removing them.

Round Brackets Create a Backreference
http://www.regular-expressions.info/brackets.html

You can read more about capture groups at the link above.

Enjoy!
Last edited by jpuziano on Tue Feb 26, 2013 3:12 am, edited 3 times in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

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

COOL!!

Post by PepsiHog » Sun Feb 24, 2013 8:03 pm

@jpuziano

BRAVO!!! Thank you very much! I really apprieciate your time and effort. The help was PERFECTION but what made the BRAVO was your clear and precise explanation of the solution.

You know that giggle you get because you understand something? That was me just now. Maybe just a few weeks or a month ago, I finally started to get the use of (). And through trials and headaches figured out the $1,$2,.... But I didn't know what you just explained (very well).

EXELLENT.

THANK YOU! :D
PepsiHog

BTW - I am giving you 20 points for this help.
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

Hello

Post by PepsiHog » Sun Feb 24, 2013 10:17 pm

@jpuziano

I can't make heads or tails of this. But if you change number to 22, the pattern doesn't work.

It seems like the pattern covers both possibilities. But if it's 22 then both matches get removed.

(\d\d:%Number%) seems like it would cover for %Number% being on the right side of the colon.

(%Number%:\d\d) seems like it would cover for %Number% being on the left side of the colon.

But that's not the case.

Does anyone know why the pattern works one way but not the other?

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

Hello

Post by PepsiHog » Mon Feb 25, 2013 12:38 am

I have been working on this now for hours. 4 or 5 hours.

WOW!! I think this actually works!!

Code: Select all

let>Number=10
let>Data=01:02;10:22;10:22;08:32;44:52;

MDL>Data

Let>pattern=(\d\d:%Number%;)(?=.*\b\1\b)|(%Number%:\d\d;)(?=.*\b\2\b)(.*)
RegEx>pattern,Data,0,match,nm,1,$3,Data

MDL>Data

I got the look ahead part from another RegEx question I had posted. I'm too tired to look up who deserves the credit for that help. But you know who you are, so consider this your credit.

Ofcourse, I have posted in the past that I figured something out, only later to find out I was wrong.

So I'd like to keep my reservation for a large plate of LOOSER, just in case I am wrong. :D ........Ofcourse, it goes without saying, I will have a large glass of Pepsi with that plate, if that's the case. :lol:

Hoping I'm right,
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

Hello

Post by PepsiHog » Mon Feb 25, 2013 2:56 am

Crud!

It only works when there is a repeated set of numbers. So since there's two sets of 10:22, it works for that. But if there's only one set it doesn't work at all.

Help?

PepsiHog

I guess I could use two RegEx statements.
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
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Feb 25, 2013 3:04 am

Hi PepsiHog,

Only have the Blackberry at the moment. Please just post an example of your data string and what you want to do to it and I'll take a look later tonight... Not a problem. :)
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Feb 25, 2013 8:38 am

Hi PepsiHog,

Interesting... First, here's the answer:

Code: Select all

let>Number=22
let>Data=01:02;10:22;08:32;44:52;10:22;

MDL>Data

Let>pattern=((\d\d:%Number%)|(%Number%:\d\d));(.*)
RegEx>pattern,Data,0,match,nm,1,$4,Data

MDL>Data
The above will work whether Number = 10 or 22 or any two digits in your data. It tested fine with 01, 02, 08, 32, 44 and 52 as well.

This was the original pattern I supplied:
(\d\d:%Number%)|(%Number%:\d\d);(.*)

The problem was the alternation specified by the | char was not being properly limited. It was either matching everything on the left of the |
(\d\d:%Number%)

...or it was matching everything on the right of the |
(%Number%:\d\d);(.*)

It worked for 10 because in your data, 10 was left of the colon : so the pattern to the right of the alternation came into play which did what we wanted:
(%Number%:\d\d);(.*)

However 22 was on the right of the colon so in that case, the pattern on the left side of the alternation came into play which was only this:
(\d\d:%Number%)

...and that did not even match the ; or the (.*) expression at all.

The solution was to add another set of round brackets to properly limit the scope of the alternation like this:
((\d\d:%Number%)|(%Number%:\d\d));(.*)

Now, either the left side of the alternation will match:
(\d\d:%Number%)

Or the right side of the alternation will match:
(%Number%:\d\d)

And this final part of the pattern will always come into play (and not get skipped because it was on the wrong side of the alternation)
;(.*)

Note also that since we added yet another set of ( round brackets ) we have to change the capture group we use in the RegEx line to get "the rest of the line" back in there... we change from $3 to $4:
RegEx>pattern,Data,0,match,nm,1,$4,Data

So sorry for providing an erroneous pattern to start with (it only worked if the number was on the right of the colon)... but this has been an instructive example showing how important it is to limit the scope of an alternation within your pattern.

Hmm... I see you tried to use Lookahead here and I wonder if it was this post you were referring to:
RegEx> example using Lookahead and Lookbehind

In any case, no need to use Lookahead or Lookbehind to solve this one... all that was needed was to properly limit the alternation.

Done... and now I think its finally time for that extra large glass of Pepsi. :D
Last edited by jpuziano on Mon Feb 25, 2013 2:57 pm, edited 1 time in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

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

Hello

Post by PepsiHog » Mon Feb 25, 2013 1:30 pm

Hi jpuziano,

Thanks again. It's funny, in other patterns I have tried placing round brackets into a double round bracket and it never was the answer. Last night, just for a moment, I considered trying that, but quickly ruled it out.

LOL - that it ended up being the answer! Ofcourse, come to think of it, I didn't think about changing the reference ($3). So it may have worked then too, but I didn't think of that either.

Headache last night? Well maybe. But I probably learned something or another. That's the good news! :lol:

OK, I can't thank you enough. You have been a great help!

Have a large glass of Pepsi, on me. (Put it on my tab.)
PepsiHog

Oh... yes. In fact, I had more than one large glass of Pepsi! Believe! That! :D

[edit] - The post you mentioned is a good post and I had not seen it before. Thanks for posting that. But the post I refered to is a post I authored.
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