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?
Regex question
Moderators: Dorian (MJT support), JRL
Re: Regex question
I would normally think that your code or at least the following code would return the results you are looking for.
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,0,,
MessageModal>m_1
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
Re: Regex question
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:
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
Re: Regex question
Thanks!
I went for this solution:
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?
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
For instance, if I wanted to capture "locked" and soAndSo?
Re: Regex question
This might help.
Good Luck,
PepsiHog
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
*/
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!
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!
Re: Regex question
You can create non-capturing groups like this:
Code: Select all
RegEx>(?<=this loan is locked by\s)(\w+),String,0,m,n,0,,