Skip to main content
Inspiring
February 18, 2021
Answered

Grep finding a phrase after the upto a specific set of words

  • February 18, 2021
  • 3 replies
  • 691 views

Hello, struggling to get my head round an expression that will do this... need to add a bold style to a couple of variations on a few words always after a 'the' for example:

take a look at the dolphin ecolocation visual

watch the dolphin hunting video

look at the dolphin ecolocation handout

I've bolded what I want to bold within the text. I've tried a few variations on positive look aheads and using an or expression to cover the various endings of the phrase i'm looking for... rough outline of where I got to below

(?=the)\w+((?i)video|(?i)visual)

This topic has been closed for replies.
Correct answer jctremblay

Hi Jean-Claude, is there a way to take into account for soft returns? I tried using \s but i'm not sure it's classed as a white space character.


Add (?s) at the begining.

(?s)(?<=the ).+?(visual|video|handout)

 

3 replies

pixxxelschubser
Community Expert
Community Expert
February 20, 2021

I would also use @jctremblay's grep, maybe modified a bit to get better performance:

 

(?s)(?<=the ).+?(handout|vi(deo|sual))

 

 

Peter Kahrel
Community Expert
Community Expert
March 13, 2021

Well, if it's performance you're after you should use non-matching subexpressions and \K instead of the classic lookbehind:

 

 

 

(?s)the \K.+?(?:handout|vi(?:deo|sual))

 

 

 

P/

hammer0909
Community Expert
Community Expert
February 18, 2021

I'm concerned that you're not providing enough context as to how your sample text will appear in the bigger picture. That being said, I've included a screen shot below of the GREP expressions that I wrote to make this happen. I'm basically just using a positive lookbehind for "take a look at the", "watch the", and "look at the", and then applying bold to everything that follows it.

Paul5CB8Author
Inspiring
February 18, 2021

Hi Chad, 

 

the sample text was just an example, the text before what I want will be hard to predict but I know that follwing a 'the' and up to visual/handout/ etc is consistent

Peter Kahrel
Community Expert
Community Expert
February 20, 2021

In that case, Jean-Claude's expression should work. Did you try it?

jctremblay
Community Expert
Community Expert
February 18, 2021

Try this one. You were missing a space after the initial  "the" and \w don't match space between words so it's better to replace it with a dot "any character". If you have use the (?i) because this could be in upper and lower case you can put it at the begining of the query.

(?=the ).+?(visual|video|handout)

 

Paul5CB8Author
Inspiring
February 18, 2021

This is bolding the 'the' as well which I don't want... 

jctremblay
Community Expert
Community Expert
February 18, 2021

Copy/Paste error... misisng <

(?<=the ).+?(visual|video|handout)