Need a bit of RegEx Help

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

Need a bit of RegEx Help

Post by PepsiHog » Wed May 30, 2012 3:42 pm

Hello Everyone,

I have actually done this by using multiple lines of code. But because I am no expert at RegEx, I thought maybe one of you might know how to do the same in just one line.

Let's say you have a string.

Let>MyString=45;23;22;45;11

Is there a pattern I can use with Regex that will return only the number or numbers that repeat.

RegEx>Pattern,MyString,0,Result,nm,0

Using the above RegEx, I would like Result_1 to be 45. Since no other numbers repeat, in this case, there would only be one result.
(The number is a variable, and can be any number.)

My macro needs to make a choice. And I want it to choose only the numbers that repeat.

Sorry. An actual example is not possible, since my macro is way to long to post.

Thanks for the assist,
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 » Thu May 31, 2012 6:23 am

Just one line huh? You're not asking for much are you... :shock:

RegEx never ceases to amaze... yes its possible... here you go:

Code: Select all

Let>MyString=45;23;22;45;11
RegEx>\b(\d+)(?=.*\b\1\b),MyString,0,Result,num_matches,0
Your line was the first one, mine was the second.

If you single step in the debugger, you'll find the following result variable created which finds the repeated value:

RESULT_1=45

Now let's add a second 11 in there and...

Code: Select all

Let>MyString=45;23;22;45;11;11
RegEx>\b(\d+)(?=.*\b\1\b),MyString,0,Result,num_matches,0
RESULT_1=45
RESULT_2=11

Is that what you were looking for?

The following code will let you quickly add in other multiple numbers to your string then test the results...

Code: Select all

Let>MyString=45;23;22;45;11;11

Let>Pattern=\b(\d+)(?=.*\b\1\b)

RegEx>Pattern,MyString,0,Result,num_matches,0

If>num_matches=0
  MDL>MyString=%MyString% and contains no repeating numbers... time for a Pepsi!
Else
  MDL>MyString=%MyString% contains %num_matches% repeating numbers and they are...
  Let>counter=0
  Repeat>counter
    Let>counter=counter+1
    MDL>RESULT_%counter%
  Until>counter,num_matches
EndIf
One caveat: if a number, let's say 45... were to appear three times in the string... you'd get 45 as a result twice.

If this is a problem for you then you could write some extra code to delete any duplicate matches or...

...it may even be possible to accomplish that within the RegEx expression itself.

If it is I'd love to see it so... open challenge... 5 rep points up for grabs.

Take care
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

not me...

Post by PepsiHog » Tue Jun 05, 2012 1:20 am

Thanks! jpuziano!

That is exactly what I need.
One caveat: if a number, let's say 45... were to appear three times in the string... you'd get 45 as a result twice.
Not a problem. Any given number can only be repeated once.
Just one line huh? You're not asking for much are you.
Nah. LOL!

Actually concerned because once I complete what I'm doing, I am hoping to share it here on the forum. It's a macro just for fun. (If I can do it!) But that's all I can say.

Thanks again!

Edit - BTW. What site do you get your info from for RegEx? The sites I go to don't seem to be that great.
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

me

Post by PepsiHog » Tue Jun 05, 2012 1:50 am

**giggle,giggle**

Thanks again!!!

I went online and, thanks to your example, I now have learned just a bit more about how to use RegEx. :D

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