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

GREP to find words before or after > symbol

Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi guys,

I wonder if you can give me a little push in the right direction with this GREP code.

I want to find words which come before/after a space and a greater than symbol.

(?<=\s>)?\w+(?=\s>)

This is what I have but it doesn't find words which come after the symbol.

Menu > Menu > Menu

Would only find the first 2 Menus.

Thanks everyone

TOPICS
Scripting

Views

12.6K

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

Guide , Jan 16, 2017 Jan 16, 2017

Hi Jakec,

Basically a GREP like

\w([^\r>]+\w)*

would do a very good job (including when the menu item contains inner space), since it captures every string that starts and ends with a word character while not containing any >.

Now, what you want is to make sure that the pattern above is either preceded by >\s, or followed by \s>, in order to exclude lines that do not contain the X > Y > … > Z structure at all. So in fact you have an alternation of two distinct regex, first based on a lookbehind, sec

...

Votes

Translate

Translate
Guide ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Jakec,

Basically a GREP like

\w([^\r>]+\w)*

would do a very good job (including when the menu item contains inner space), since it captures every string that starts and ends with a word character while not containing any >.

Now, what you want is to make sure that the pattern above is either preceded by >\s, or followed by \s>, in order to exclude lines that do not contain the X > Y > … > Z structure at all. So in fact you have an alternation of two distinct regex, first based on a lookbehind, second based on a lookahead:

The lookbehind form is:

(?<=>\s)\w([^\r>]+\w)*

The lookahead form is:

\w([^\r>]+\w)*(?=\s>)

So your final GREP is:

((?<=>\s)\w([^\r>]+\w)*)|(\w([^\r>]+\w)*(?=\s>))

@+

Marc

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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Marc,

Thanks for your reply, however your GREP seems to miss out the first word in the string.

Menu > Menu > Menu

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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

(?<=\s>\s)?[^\s>]\w+(?=\s>\s)?

This find all of them but because I use the ? it find all other paragraphs too

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi,

Do you want to find Soft Menus and Submenus in text, presented as … > … >… > …?

… as:

Capture d’écran 2017-01-16 à 12.53.02.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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Obi,

I'm trying to select only the text between the space and > symbols.

I want to make the text bold.

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Could you make a screenshot of real texts?

(^/)

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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

So I want to change text like this to bold (not including the >)

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Here, specifically:

\u\l+(?=\h>)|(?<=>\h)\u\l+

(^/)

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
Guide ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

> however your GREP seems to miss out the first word in the string (…)

Works for me though, even as GREP-style:

GREP-Test.png

Make sure you have properly encoded the regex. (Keep in mind that backslashes are needed in JS strings for escaping purposes.)

@+

Marc

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 ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Marc,
I also tested your GREP with a GREP Style.

No problem with InDesign CC 2017.

Regards,
Uwe

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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Marc,

Very sorry, I must have missed something when I copied your code.

It does work That's great thanks for your help.

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
Engaged ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Marc, I'm writing an article on the use of GREP in InDesign, do you mind if I use this expression as an example?

I'm happy to reference your website or this forum

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
Guide ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

You're welcome, Jakec. Feel free to share 😉

@+

Marc

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
Guide ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

And now a short snippet to test my GREP as well:

// FindChange Settings

// ---

var FIND_WHAT = "((?<=>\\s)\\w([^\\r>]+\\w)*)|(\\w([^\\r>]+\\w)*(?=\\s>))";

var CHANGE_FONT = "Arial\tBold";

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = FIND_WHAT;

app.changeGrepPreferences.appliedFont = CHANGE_FONT;

// Assuming a TextFrame is selected.

// ---

var target = app.selection[0].parentStory.paragraphs.everyItem();

target.changeGrep();

@+

Marc

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Marc,

Just for comment!

Doable if first line case, not in the other lines!

Capture d’écran 2017-01-16 à 14.15.17.png

I'ld use that code to get this:

Capture d’écran 2017-01-16 à 14.15.36.png

Grep code:

\u[\l\d\h-]+(?=\h>)|(?<=>\h)\u[\l\d\h-]+[…~j]?

or

(\u[\l\d\h-]+)(?=\h>)|(?<=>\h)(?1)[…~j]?

(^/)

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
Guide ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Obi,

I made no assumption about what characters are supposed to form a Menu string since it wasn't specified in the original question. My solution is just about splitting a string along a " > " separator, as originally requested. Now of course many refinements could be proposed, depending on what the user actually wants to do, in what context, what Unicode range, and so on.

Marc

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Seems to be more complete:

(\u[^\u…~j]+[…~j]?)(?=\h>)|(?<=>\h)(?1)

Works taking in account only one beginning cap.! 

(^/)

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 ,
Mar 23, 2017 Mar 23, 2017

Copy link to clipboard

Copied

Hi, I'm looking for something similar.  I need a grep code that will bold text (of the same character style) after the second space.  Another way to do it is to bold any text that comes after the Article #.

Article 2 District Standards

Article 3 Building Standards

Thanks! 

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 23, 2017 Mar 23, 2017

Copy link to clipboard

Copied

^Article \d+\K.+

(\K stands for 'match but do not capture, it's a variable-length lookbehind.)

Peter

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 ,
Mar 23, 2017 Mar 23, 2017

Copy link to clipboard

Copied

Thank you so much!!

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 ,
Mar 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

Follow-up question.  How would I adjust the greb to allow for the condition below.  Thanks!

Article 3 Building Standards //  Section 3.A General Building Standards

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 24, 2017 Mar 24, 2017

Copy link to clipboard

Copied

If you mean 'Article 3' OR 'Article 3.A' ( where 3.A could be 3.B, 3C, etc)., then try this:

^Article \d\d?(\.[A-Z])?\K.+

that is, Paragraph-initial 'Article'

followed by a space and one or two digits

followed optionally by a dot and a captital

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
Community Beginner ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

So, this didn't seem to work.  The following is what I'm trying to accomplish.  The first code from above (^Article \d+\K.+) works on the text prior to the "//".  But the other code (^Article \d\d?(\.[A-Z])?\K.+) isn't resulting in no-bold and bold text, as desired.  What would be the compound formula for this entire series of text?  Thanks so much.  

Article 2 Character District Standards  //  Section 2.A General District Standards

Article 3 Building Standards  //  Section 3.A General Building Standards

Article 3 Building Standards  //  Section 3.B Primary Building Types

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
LEGEND ,
Mar 27, 2017 Mar 27, 2017

Copy link to clipboard

Copied

Knowing you use double spaces before/after "//":

(Article|Section)\h\d+(\.[A-Z])?\h\K[^/\r]+(?=\h\h//|$)

(^/)

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