Regex question

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
NancyM
Pro Scripter
Posts: 66
Joined: Mon Jul 18, 2016 7:01 pm

Regex question

Post by NancyM » Wed Aug 31, 2016 10:00 pm

I want to extract a word out of this string

String = This loan is locked by soAndso.

I want to extract soAndso if the string matches: This loan is locked by...

When I've used regexp in the past I'd usually just enclose the part I want to capture in parens, like this:

pattern=This loan is locked by (\w+)

However, I end up with the entire pattern not the part in parenthesis. How do I do this using MS regex?

dtaylor
Junior Coder
Posts: 46
Joined: Mon Aug 08, 2016 2:42 am

Re: Regex question

Post by dtaylor » Thu Sep 01, 2016 2:52 am

I would normally think that your code or at least the following code would return the results you are looking for.

Code: Select all

Let>String=This loan is locked by soAndso
RegEx>(?:this loan is locked by\s)(\w+),String,0,m,n,0,,
MessageModal>m_1
But, that is not the case. In the above RegEx, matching group #1 is supposed to be a non-capturing match, yet it is still returned in the match results.

The only way I have been able to get the RegEx results you are look for is use RegEx Replace as follows.

Code: Select all

Let>String=This loan is locked by soAndso
RegEx>(?:this loan is locked by\s)(\w+),String,0,m,n,1,$1,MatchedText
MessageModal>MatchedText

hagchr
Macro Veteran
Posts: 277
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Regex question

Post by hagchr » Thu Sep 01, 2016 7:04 am

Hi, The pattern shows what you are looking for so in both cases you are actually looking for the whole string

pattern=This loan is locked by (\w+)
pattern=(?:this loan is locked by\s)(\w+)

(?:this loan is locked by\s) still tries to match "this loan is locked by" followed by \s

To solve it, one approach could be what dtaylor suggests, to work with replacement (replace the full string (ie everything) with the text captured in (\w+)), alt just replace what you do not want. Another approach is to match into m_1 see code:

Code: Select all

//Replacement into string
//========================

//Use RegEx>
Let>string=This loan is locked by soAndso
Let>Pattern=This loan is locked by (\w+)
RegEx>Pattern,string,0,m,nm,1,$1,string
MDL>string

//Use StringReplace>
Let>string=This loan is locked by soAndso
StringReplace>string,This loan is locked by ,,string
MDL>string

//Match into m_1
//===============
//Use \K to discard what has been matched so far
Let>string=This loan is locked by soAndso
Let>Pattern=This loan is locked by \K\w+
RegEx>Pattern,string,0,m,nm,0
MDL>m_1

//Use look-behind to match a position
Let>string=This loan is locked by soAndso
Let>Pattern=(?<=This loan is locked by )\w+
RegEx>Pattern,string,0,m,nm,0
//This effectively matches a position (with certain text to the left of the position) followed by word characters
MDL>m_1

NancyM
Pro Scripter
Posts: 66
Joined: Mon Jul 18, 2016 7:01 pm

Re: Regex question

Post by NancyM » Thu Sep 01, 2016 1:52 pm

Thanks!

I went for this solution:

Code: Select all

//Match into m_1
//===============
//Use \K to discard what has been matched so far
Let>string=This loan is locked by soAndso
Let>Pattern=This loan is locked by \K\w+
RegEx>Pattern,string,0,m,nm,0
MDL>m_1
Additional question: Is it possible to expand on this to capture more than one match into the array?

For instance, if I wanted to capture "locked" and soAndSo?

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

Re: Regex question

Post by PepsiHog » Mon Sep 19, 2016 3:34 am

This might help.

Code: Select all

//Match into m_1
//===============
//Use \K to discard what has been matched so far
Let>string=This loan is locked by soAndso
//Let>Pattern=This loan is locked by \K\w+
//RegEx>Pattern,string,0,m,nm,0
//MDL>m_1

RegEx>(.*)\bis\b(.*)?by\b(.*),string,0,match,nom,1,$2 $3,Result

mdl>%Result%

/*
() = a group to refer back to.
$1,$2, $3.... = group that has what you want to capture.
   -groups are counted from left to right
   -to count complex groups such as =====>(by(.*)is(a test))
        - the first outside bracket is $1 ^
        - $2 is the next inside bracket. ====^
        - $3 is the next one from $2  =============^
        - you always count from left to right.
()? = group is optional, does not have to be found for a match. You could replace this with...

   (locked)?      

I don't know your purpose, so just guessing at what you need.
        
\b = word boundary or space
*/
Good Luck,
PepsiHog
Windows 7

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

NickD
Pro Scripter
Posts: 53
Joined: Fri Sep 23, 2016 2:17 pm

Re: Regex question

Post by NickD » Fri Jun 08, 2018 2:20 pm

You can create non-capturing groups like this:

Code: Select all

RegEx>(?<=this loan is locked by\s)(\w+),String,0,m,n,0,,

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