Skip to main content
Inspiring
January 16, 2017
Answered

GREP to find words before or after > symbol

  • January 16, 2017
  • 1 reply
  • 15160 views

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

This topic has been closed for replies.
Correct answer Marc Autret

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

1 reply

Marc Autret
Marc AutretCorrect answer
Legend
January 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, 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

Inspiring
January 16, 2017

Hi Marc,

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

Menu > Menu > Menu

Inspiring
January 16, 2017

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

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