GREP to replace space with nonbreaking space

Learn / Forums / General InDesign Topics / GREP to replace space with nonbreaking space

Viewing 2 reply threads
  • Author
    Posts
    • #66366

      So, I have some study guides with lots of equations. The material has a single space on either side of the operators, which makes the spaces easy to search for and replace with nonbreaking spaces, per the production manager’s request.

      My GREP is:

      Find:\s\+\s|\s=\s|\s~=\s|\s?\s|\s÷\s

      Change:~S$0~S

      This finds everything, no problem. But when I replace the spaces with nonbreaking spaces, the original spaces remain, so I get a nonbreaking space, a space, the operator, a space, a nonbreaking space. I tried separating everything with parentheses and changing the Change to ~S$2~S, but then nothing is found, so that’s moot.

      Of course, I made a search that will replace the ~S\s with ~S and then one that will replace the \s~S with ~S, but this seems unwieldy. What am I missing here?

      Any advice is greatly appreciated!

    • #66378

      You get your original spaces back because ‘$0’ inserts the exact entire found text again. You can use parentheses to indicate a part of the search string, and then use ‘$1’ to insert the first group in the Replace field, ‘$2’ for the second, etc. — up to 9 groups. But of course you can only ‘recall’ a numbered group in Replace if you defined it with parentheses in the Search field (and I bet that’s why your own attempts with ‘$2’ failed).

      A second change is you do not have to list all of your search strings separately; after all there has to be only ONE character, out of a certain limited set, between your spaces.

      All together, you can search for this

      \s([+=~=×÷])\s

      and replace with

      ~S$1~S

      There was a single ‘?’ in your search query, and checking the rest I suppose it’s a copy-paste problem and it should have been the ‘multiply’ operator × instead. I would also rather use a *real* mathematical minus ‘-‘ (U+2212) rather than an en-dash, because it aligns with the ‘+’ and is a bit thicker (then again, not all fonts contain it).

    • #66387

      Ah, I was making things more difficult than they needed to be. Thank for you for this! Yes, the ? is the multi sign (U+00D7). I agree about the mathematical minus. The authors are used to doing everything with Times in Word, so that’s how they “set” their type in ID. The good thing is that they did this consistently, so it’s a reliable S&R.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
>