You must be logged in to post Login

Search Forums:


 






Can GREP remove hyphens between characters and put the characters back in?

UserPost

12:49 pm
September 8, 2011


Chuckie

Bainbridge Island, WA

Member

posts 37

I have a what appeared to be an easy one that I cant find on Google or with my book on GREP by Kahrel.

Gobs of messy text that has about 2 words per graf in which a word is broken by a hypen.   it is not hyphenated, but rat-her bro-ken like this.

So I initially wanted to search for / any lowercase letter / followed by a hyphen / followed by a lowercase letter

I want to replace it with the front & back lowercase letters it found and delete the hyphen.   ID offers us "Found Text"  as a retrieval but how do we tell it to remember what it's found?

3:07 pm
September 8, 2011


Jongware

Member

posts 764

That's what Numbered Groups are for. Search for this:

(\l)-(\l)

and replace with

$1$2

But it can be done with a bit more flair — by not 'finding' the lowercase characters at all! (Don't worry, I'll walk you thru it.)

For that, you have to visualize how a regular Find works: you type in what needs to be found, press the Find button once, and InDesign selects the 'found text'. What if you coud find only the hyphens which have a lowercase character before and after it? — Notice how this is distinctly different from what you do right now, that is, looking for the exact sequence "lowercase – hyphen – lowercase"!

This lil' bit of magic can be done using "lookbehind" and "lookforward" sequence. Inside the "lookbehind" sequence you can specify what needs to be to the left of 'the text you actually found'. This fragmentatte:

(?<=\l)-

will only find-and-select individual hyphens that are preceeded by a lowercase character. A phrase worthy of emphasizing.

Simlarly, this one:

-(?=\l)

will only find-and-select hyphens that are followed by a lowercase character. And yes! you can combine these two:

(?<=\l)-(?=\l)

will only select hyphens that … well, see the above :) Replace with Nothing-At-All, and thus you'll be effectively removing the hyphens without touching anything else.

So what is the advantage of this method? In this case, it's rather miniscule. InDesign will not 'touch' the characters left and right of the hyphen; in particular, it will not "remove" them and then "re-insert" them again for the Replace operation. (I think this is rather how Search-and-Replace works, internally.) So it'll run fractionally faster. But consider a situation where you want to apply formatting to these occurrences of hyphens only when preceeded and succeeded by lowercase. You cannot do this with regular Search/Replace, and nor with the Numbered Grouping method. But with the lookbehind and lookahead function means you can!

3:13 pm
September 8, 2011


Chuckie

Bainbridge Island, WA

Member

posts 37

Jongware, you are my idol!!!

I was just about to post this  –  (w)-(w) for $1$2  – after an exhaustive search.   But I would have had to explain what it didn't do.  

Then you ring in with the home run.  :-)   You have such a gift, I could see you outting a book of great GREPs and what they did.  Lay my $$ right here.

17 thank you's…

3:24 pm
September 8, 2011


Jongware

Member

posts 764

Aw shucks. De nada.

3:30 pm
September 8, 2011


Chuckie

Bainbridge Island, WA

Member

posts 37

Reading along with your other posts, tracking your website pointers for everyone, you really set the standard for giving.

When I find something cool, it's a reminder to self to post what you've learned to give back.  And it's dudes like yourself who've set the tone.

I grovel endlessly in thanks….