• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

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)

TOPICS
How to , Scripting , Type

Views

287

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 19, 2021 Feb 19, 2021

Add (?s) at the begining.

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

 

Votes

Translate

Translate
Community Expert ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

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)

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Copy/Paste error... misisng <

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

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

\n is a soft return character. 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

Add (?s) at the begining.

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

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.

ForumsGREP.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2021 Feb 20, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2021 Feb 20, 2021

Copy link to clipboard

Copied

I would also use @Jean-Claude Tremblay's grep, maybe modified a bit to get better performance:

 

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 13, 2021 Mar 13, 2021

Copy link to clipboard

Copied

LATEST

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/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines