String help

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
jsmyong55
Newbie
Posts: 18
Joined: Thu Aug 29, 2013 4:14 am

String help

Post by jsmyong55 » Sun Sep 01, 2013 5:23 pm

I need to read from a set of strings, eg EBB21-Jason or EBB203-Jason but I want to be able to capture only the code in front (EBB21 and EBB203 respectively). Which means I would have to capture any characters before "-". What is the simplest way to do that? Do I use regex? I am not familiar with regex though.. Thanks

jsmyong55
Newbie
Posts: 18
Joined: Thu Aug 29, 2013 4:14 am

Post by jsmyong55 » Sun Sep 01, 2013 5:41 pm

Better yet if I am able to capture different variations entered by user, say EBB12-Jason , EBB12:Jason, EBB12 Jason. I guess I will limit it to 3 different variations "-" , ":" and " ". Too many and I will have a hard time. EBB12 is an ID by the way.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Sep 02, 2013 8:44 am

You can either do it with simple substring handling:

Code: Select all

Let>example_string=EBB12-Jason

//get everything up to the "-"
Position>-,example_string,1,posHyphen
MidStr>example_string,1,{%posHyphen%-1},code_part
MessageModal>The code is %code_part%
Or quicker with RegEx:

Code: Select all

Let>example_string=EBB12-Jason

//get everything up to the "-"
RegEx>.*(?=\-),example_string,0,matches,nm,0
MessageModal>The code is %matches_1%
See:
http://www.mjtnet.com/forum/post32883.html
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

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

Post by hagchr » Tue Sep 03, 2013 7:07 am

Small adjustment to cover either of "-", ":", or " ". Since you are new to RegEx, it is very powerful, but you have to be careful how you structure them so you don't get wrong results. The code below would work well for simple strings, starting with the ID. (If you have longer strings with several "-", ":" or " ", or the ID somewhere in the middle, you would get strange results and would have to modify the criteria.) You can find a free good tutorial to RegEx under

http://www.regular-expressions.info/



Let>example_string1=EBB12-Jason

//get everything up to the "-", ":", or " "
RegEx>.*(?=[-: ]),example_string1,0,matches,nm,0
If>nm>0
MessageModal>The code is %matches_1%
Endif

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