Avatar of Svgmassive
Svgmassive
 asked on

Extract exact word with a trailing space

i would like to extract the first Rem that has a space after from the string in regex
 "RemLETS BUILD THE': Rem Room"
in the example Room should be the extracted word
 "Rem LETS BUILD THE': Rem Room"
in the example the entire sentence from LETS BUILD THE': Rem Room"
ignore case false
Regular ExpressionsMicrosoft Access

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Dr. Klahn

Depends on what regex engine is in use, but this would be a commonly used form:

Rem\s(.*)

Open in new window


Match "Rem", match a whitespace, and gobble up the rest.
Bill Prew

Depending on your comfort level with RegEx, the above posted solution captures the extracted data you want in a "capturing group", so you don't want the full match, you want the first captured group.  Different languages or environments will reference that different ways.

Also, in case you haven't seen it, there is a pretty good online site when you ar developing RegEx expressions.  It lets you create a RegEx to test, and feed it some test data.  It breaks down the RegEx pattern into something a bit more descriptive so you can confirm it's doing what you want, and shows you the result.

Here's an example of your question with the above solution being tested.



»bp
Svgmassive

ASKER
Debug.Print .Replace(ReplaceValue, "")=RemLETS BUILD THE':
 Debug.Print .Replace(ReplaceValue, "$1")=RemLETS BUILD THE': Room
it's not doing it in VBA
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bill Prew

So, what exactly are you trying to accomplish.  The question asked to "extract" the text after the first "Rem ", but now it looks like you are trying to replace that?  Can you clarify?

It's a bit more involved in VBA since it doesn't have built in RegEx function, but we can work around that fairly easy using the scripting RegEx object.  But before I post a sample I need to know what you want the result to be.


»bp
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Svgmassive

ASKER
Great this did it also different style "^(.*?)\bRem\b"
thanks
Bill Prew

Welcome.


»bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.