Help with Regex

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Scones
Junior Coder
Posts: 32
Joined: Fri Jul 05, 2019 11:21 am

Help with Regex

Post by Scones » Thu Nov 07, 2019 11:51 am

Hello

Can anyone figure this one out? Here is my string:

Esko ArtiosCAD - [TEST - 0 mfg]

This text is always the same
This text is always different
This is the text i need to get

I have never used regex before, so i'm very lost here.

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

Re: Help with Regex

Post by Dorian (MJT support) » Thu Nov 07, 2019 1:06 pm

This regex returns "everything between"

Code: Select all

//To help you understand
let>mystring=Esko ArtiosCAD - [TEST - 0 mfg]
let>BEGINNING=0 
let>END=]
RegEx>(?<=%BEGINNING%).*?(?=%END%),mystring,0,matches,nm,0
MessageModal>%nm% matches %CRLF%The item number is %matches_1%

//All condensed into one line
RegEx>(?<=0 ).*?(?=]),Esko ArtiosCAD - [TEST - 0 mfg],0,matches,nm,0
MessageModal>%nm% matches %CRLF%The item number is %matches_1%
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Help with Regex

Post by Grovkillen » Thu Nov 07, 2019 5:29 pm

A piece of advice, pay for regexBuddy
Let>ME=%Script%

Running: 15.0.24
version history

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

Re: Help with Regex

Post by Dorian (MJT support) » Mon Nov 11, 2019 12:01 pm

Grovkillen wrote:
Thu Nov 07, 2019 5:29 pm
A piece of advice, pay for regexBuddy
Also, here's a free Regex course. It helped me grasp a basic understanding.

https://www.udemy.com/course/regex-acad ... g-sorcery/
Yes, we have a Custom Scripting Service. Message me or go here

Scones
Junior Coder
Posts: 32
Joined: Fri Jul 05, 2019 11:21 am

Re: Help with Regex

Post by Scones » Mon Nov 18, 2019 2:26 pm

Dorian (MJT support) wrote:
Thu Nov 07, 2019 1:06 pm
This regex returns "everything between"

Code: Select all

//To help you understand
let>mystring=Esko ArtiosCAD - [TEST - 0 mfg]
let>BEGINNING=0 
let>END=]
RegEx>(?<=%BEGINNING%).*?(?=%END%),mystring,0,matches,nm,0
MessageModal>%nm% matches %CRLF%The item number is %matches_1%

//All condensed into one line
RegEx>(?<=0 ).*?(?=]),Esko ArtiosCAD - [TEST - 0 mfg],0,matches,nm,0
MessageModal>%nm% matches %CRLF%The item number is %matches_1%
Thank you Dorian. Very helpful.

One more question: How would you write it, if you wanted to define "End" like you did, but instead of defining the Beginning, you just want to start from the beginning of the string?

This question is unrelated to my first question. I don't need to do that in that code .

Grovkillen wrote:
Thu Nov 07, 2019 5:29 pm
A piece of advice, pay for regexBuddy
Thanks guys. I will try the course and will consider buying regexbuddy.

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

Re: Help with Regex

Post by Dorian (MJT support) » Mon Nov 18, 2019 2:44 pm

So "everything in front of" instead of "everything between"?. Maybe one of the Regex gurus in here can show you that in Regex, but you don't need regex for that. Macro Scheduler can do that with Position, Length, and Midstr.

Get everything before G :

Code: Select all

Let>MyString=ABCDEFGHIJKLMNOPQRSTVUWXYZ

Len>MyString,MyStringLen
Pos>G,MyString,1,PosChar,
Midstr>MyString,1,{(%Poschar%-1)},MyNewString

MessageModal>MyNewString
Yes, we have a Custom Scripting Service. Message me or go here

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

Re: Help with Regex

Post by PepsiHog » Wed Nov 20, 2019 4:19 pm

@Scones,

Here is a RegEx that will grab what you need.

Code: Select all

let>mystring=Esko ArtiosCAD - [TEST - 0 mfg]

RegEx>^.* \[.* - 0 (.*)\],mystring,0,match,nom,1,$1,TextYouNeed

mdl>%TextYouNeed%

^ = start of string
.* = every character 0 or more times (up to the next specified character) Which is [.
\[ = Because RegEx uses [ in arguements, we use \ to let regex know it's part of what we are looking for.
.* = every character 0 or more times (up to the next specified character)
- 0 = text we are expecting. Literal.(There is a space before the hyphen(-). A space is a character.)
() = tells regex to remember what is between these parentheses. It's called a group.
(.*) = remember everything in this group (up to the next specified character) Which is ].
\] = same as [

Hope this helps,
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!

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