Copy link to clipboard
Copied
I need help creating a GREP expression that will change the formatting of the first two words of a paragraph including a number that may or may not be following (e.g. Executive Director, Executive Director 2).
Any help would be appreciated.
Thanks much.
^.+?\s.+?\s\d?
Copy link to clipboard
Copied
^.+?\s.+?\s\d?
Copy link to clipboard
Copied
Thanks Michael. It works perfectly, and it seems so easy, now that I see your formula.
Sure appreciate your help.
Copy link to clipboard
Copied
Hi,
Not correct!
[CS6+]
^(\H+)\h(?1)(\h(?=\d)\d)*
(^/)
Copy link to clipboard
Copied
Thanks for your reply Obi-Wan. I tested your GREP solution as well, and it also worked as expected.
Both your solution and Michael's solution behave the same (at least for my particular needs).
I'm eager to better understand GREP, so perhaps you could elaborate on why you feel your answer is correct, yet Michael's is not?
Thanks much.
Copy link to clipboard
Copied
Of course, Michael's code seems to work because his way is just enough in your case but, as I show, his code includes the "space" after the two words if no number (+ a space before) after them!
It could be problematic if you really need to find these two words and only them!
For instance, to make an index. In this case, the entry would include this "space"
If you wanna play with Grep, keep in mind that you'll need to be "precise"!
In any case, by accepting it, be aware of your "vagueness" and its possible consequences!
Another writing [I like!]:
^(\H+)\h(?1)(?>\h\d+)?
(^/)
Copy link to clipboard
Copied
Vagueness is kind of nice; you should try it sometime!
Obi-wan, in your GREP, I am not familiar with \H+. Does that mean "anything NOT a horizontal space one or more times", effectively a word? And what does h(?1) mean? Does that mean ONLY one space? And what does the third grouping mean?
Copy link to clipboard
Copied
Hi Michael,
To be really precise, we would use this:
^([\u\l]+)\h(?1)(?>\h\d+)?
What does it mean?
^
--> Beginning of para
([\u\l]+)
--> any group of letters (no space, no number) -- see below why this writing is better!
\h
--> horizontal space (12 kinds of space + tab)
(?1)
--> that means the same kind of group (delimited by parenthesis) than the first one, so: ([\u\l]+)
(?>\h\d+)?
--> interesting writing: '(?> … )' is an "unbreakable" entity! with the "?" behind it, we means: "exist or not".
the targetted unbreakable group is: '\h\d+'
Using this code will avoid the problem you have with your code.
So, my code has 4 parts: a word + a space + a word (+ in option: a space + a number)
Using ([\u\l]+) will avoid to take in account: "Director 2" at a para beginning.
Simple!
I like precision but, I repeat, your code could be enough here!
(^/)
Copy link to clipboard
Copied
Obi-wan, thanks for the detailed breakdown and explanation. We all appreciate your "precision" and expertise.
Copy link to clipboard
Copied
End of your training!
Risky!
Less risky! ^(([\u\l-])+)\h(?1)(?>\h\d+(?!\H))? …but really less readable!
… Just for fun!
(^/)
Copy link to clipboard
Copied
Obi-wan, thank you for that instruction. I feel a bit smarter, now. My Vague-O-Meter needle has swung a bit to the right!