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

GREP for more than one word in a line

Return to Member Forum

  • Author
    Posts
    • #56755
      dtodd
      Participant

      Hi,

      I am a newbie with using GREP.

      I have set up a paragraph style for bold text and I need to know what to put in GREP style. This is what I'm looking to do:

      bold text (one word or more), en space, plain text, tab, bold text (one word or more), en space, plain text

      I have tried using w+~> which works, except if there is more than one word, only the word just before the en space is bold.

      If I use w.+~> then ALL the words are bold up to the second en space.

      What should I use?

      Thank you!

    • #56756
      dtodd
      Participant

      I didn't realize that the forum would remove the backslash from in front of the “w”.

    • #56758

      w (type double backslashes in this forum ;)) doesn't really match a 'word', but just one 'word character'. According to InDesign, word characters include 0-9, a-z, A-Z, Cyrillic, Greek, and possibly other scripts as well (Hebrew, Arabic, Thai, Chinese and whatnot) and all accented variants of them. It noticeably does not include the common hyphen, so if you might have one in one of your words, you need to adjust your GREP.

      The problem you are facing is that GREP is GREEDY by default: it tries to grab as much as possible, but always within the constraints you put in. That's why the seemingly straightforward “w.+~>” will capture anything up to the very last en space. Everything inbetween is matched because the period is 'any character at all', which is what gets repeated — in effect, it scans for the very first word character it can find, then matches everything up to the very last en space.

      Fortunately, there is a toggle to make it less greedy: add a question mark after the repeat character. In addition, since the space is not a “word character” either, you need to specify that's valid too.

      This ought to work better:

      [w ]+?~>

      which in plain English translates to “either a word character or a space, repeated as little as possible before encountering an en space”.

      I'm a fan of matching as little as possible; in your case, it would match an optional initial space as well as the final en space. That's to be frowned upon, because imagine you want to apply underline instead of bold; then you'd see too much is marked. (I test my GREPs with Underline rather than Bold, so I can see what spaces get picked up.)

      This somewhat more complicated GREP will do a neater job, because it only starts at a proper word character (not “word or space”, which picks up the first space) and checks for but does not include the final en space. I also added the hyphen in the repeating OR-set (it's a special character inside an OR set, so it needs escaping to get an 'actual' hyphen).

      w[w- ]*?(?=~>)

    • #56786
      dtodd
      Participant

      Thank you, thank you!

      I tested what you described using the underline to see what space is included. I will keep that in mind in the future.

      The code worked great. I don't have hyphenated words, but I included all of the code in my GREP style… because of course, you never know when something will be added that has a hyphen.

      Thanks again.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Forum Ads