ReadLn Everything After ":" Colon

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
moonspoon
Newbie
Posts: 1
Joined: Thu Mar 14, 2019 10:07 pm

ReadLn Everything After ":" Colon

Post by moonspoon » Fri Mar 15, 2019 9:34 pm

When using ReadLn, how would I select everything after a colon ":" and leave everything before it out of the variable? Reading through the forum I see using Regex as the solution but I'm unsure of the usage. Here is my code:

Let>RP_WAIT=1
Let>RP_ADMIN=1
Run>cmd.exe /c systeminfo >%TEMP_DIR%\sysinfo.txt
ReadLn>%TEMP_DIR%\sysinfo.txt,13,strLinemanufacure
ReadLn>%TEMP_DIR%\sysinfo.txt,14,strLinemodel

MessageModal>%strLinemanufacure%_%strLinemodel%

Any help would be great! Thank you!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 394
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: ReadLn Everything After ":" Colon

Post by Dorian (MJT support) » Sat Mar 16, 2019 11:56 am

Hi MoonSpoon!

Here are three ways of doing this. My third solution is intended to help you understand some of the string handling functions in Macro Scheduler.

//Use Regex

Code: Select all

Let>MyString=1234567890:QWERTYUIOP
RegEx>(?<=\:).*,MyString,0,matches,num,0
MessageModal>matches_1
//Use Separate

Code: Select all

//Set up a sample string
Let>MyString=1234567890:QWERTYUIOP

//Use Separate
Separate>MyString,:,NewString

//If there's only one colon, everything before the colon will be in in NewString_1, and everything after in NewString_2
MessageModal>%NewString_1%
MessageModal>%NewString_2%
OR...

//More string handling examples to do the same thing.

Code: Select all

//Set up a sample string
Let>MyString=1234567890:QWERTYUIOP

//...we could use Position,then do some calculations

//Get the length of the string
Length>MyString,LenMyString

//Find the position of the colon
Position>:,MyString,1,ColonPos

//The part we're looking for is 1 position on from the colon
Let>AfterColonPos=ColonPos+1

//How long is the string we're looking for?
Let>StringAfterColon=LenMyString-ColonPos

//Now we know where everything is, extract that part of the string
MidStr>MyString,AfterColonPos,StringAfterColon,NewString

MessageModal>NewString
Always accepting custom script commissions. Message me or go here

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

Re: ReadLn Everything After ":" Colon

Post by PepsiHog » Sun Mar 17, 2019 10:43 pm

@MoonSpoon

//Read your line.
ReadLn>YourPath\filename,3,nLine
//Remove everything before colon and the colon itself.
RegEx>(.*):(.*),nLine,0,match,nom,1,$2,RestOfLine
mdl>%RestOfLine%

The quotes create what is called a group. (.*) - This means put everything in a group.(.*=everything)
So it does that. But then the colon is outside that group. So, the match is only up to the colon not including the colon.
Now, the colon is used in the argument already. So the next group will be everything after the colon.
So... the next group says put everything else here.

Groups are referred to in the order that they occur. The first group is the first instance of (.*). The second is the second (.*).

.........nom,1,$2,RestOfLine

The 1 indicates you want to do a replacement.
The $2 indicates your replacement is group 2.
"RestOfLine" is the new string or replacement.(the result)

You have a basic idea even if you don't fully understand. Type out this RegEx and look at the command reference that tells what to type next. (at the bottom of the editor text screen)

match is an array. match_1,match_2.....

Each match that RegEx> finds will be an array. You do not have to use the word match. What ever suits you.

nom is the number of matches. I use nom. But again use what ever you like.
MS v14 Windows 10

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2018) 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