Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Replace character between any digit withouf effecting the digits

Return to Member Forum

  • Author
    Posts
    • #64083

      Hi!

      I have a book with several occurrences of year-year (1890-1920). Almost all of these are using – instead of – or —. To find the events I use ^9-^9, but I only want to replace the character in-between the digits without effecting the digits. Is there a solution to this?

    • #64084

      For that you need to use the GREP tab of the Search-and-Replace dialog. Switch to that, then enter

      (?<=d)-(?=d)

      in the Find What field, and

      ~=

      in the Replace With field.

      GREP is about halfway between a regular text search and a full programming language; certain characters are found 'as usual', where other single or multiple character sequences are actually codes that trigger some specific behavior. For GREP, the special codes for 'any digit' is d and it works just like the regular ^9, but the extra codes around it: (?<=..) and (?=..) form Lookbehind and Lookahead sequences. These do the magic: the text inside is matched (i.e., it should be found) but it is not included in the Replace operation. So whatever digits are found, they are never removed; only the single hyphen inside is actually replaced with whatever you put in the Replace With field.

      See FindBetween: A Useful GREP String for a couple more examples, and the overview page GREP for a list of relevant pages.

    • #64085
      David Blatner
      Keymaster

      There are two ways to do this. Sandee Cohen taught me the first years ago, before grep: You search for ^9-^9 in the regular Text tab of the find/change dialog box and apply a character style. Then do another search for – (hyphen) in that particular character style and change it to an en dash. (Then you can find/change a third time to remove the character style if you want.)

      But the better way, now that we have grep, is to switch to the grep tab of the find/change dialog box and search for:

      (?<=d)-(?=d)

      and replace with the en dash. That code searches for any hyphen that has a digit before and after.

    • #64086
      David Blatner
      Keymaster

      Drat! Theun beat me to it… you're just too fast! :)

      People are jumping out of their seats to help here at indesignsecrets!

    • #64094

      David, nothing makes me sit quicker up than a chance to show off GREP to someone for the first time. :)

      Pre-GREP, I used a slight variation on Sandee's trick — I used to create a custom color swatch, so I could see what was happening, and when finished all I had to do was delete the swatch and replace it with black. A three-step approach — when GREP was added to ID's toolkit this became a one-step process.

      This particular search has been in my FindChangeByList list for years. I also have one that works for roman numbered ranges …

Viewing 4 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads