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 – find first space

Tagged: 

Return to Member Forum

  • Author
    Posts
    • #75950
      Jahrod
      Member

      I want to find the first White Space in paragraphs and then add a Forced Line Break. The problem I get is it adds Forced Line Break to all spaces in my paragraph, where as I only want it to look for the FIRST and ONLY FIRST White Space in my paragraphs.

      For instance:

      ‘Today I want to be in the sun’.

      Should change to:

      ‘Today
      I want to be in the sun’.

      I’m quite sure this is possible but I cannot figure it out.

    • #75952
      Justin Sligh
      Member

      Jahrod,

      Try the grep

      (^.+?)(\x20)

      Replace with:
      $1

      I was unable to find only the first whitespace character with positive lookbehind or \K. Might just be a lack of coffee. Instead, your grep looks for the first instance of one or more characters followed by a whitespace. You then break that up into two found sets using parentheses. For the replace, you call the first found set using $1 and then the forced line break

    • #75953
      Eugene Tyson
      Member

      If I understand correctly

      ^ +

      Replace with

    • #75965
      Jahrod
      Member

      Thank you, Justin, that works a treat! How exactly is that code working? I especially don’t get what the ’20’ is doing!?

      • #75970
        Justin Sligh
        Member

        Jahrod,

        (^.+?)(\x20)

        \x20 refers to unicode for normal space created by the keyboard’s spacebar (U+0020). includes breaking spaces, which we do not want.

        ^ Beginning of paragraph.
        . Any character
        +? One or Many times (Shortest Match)

        () Parenthese encapsulates found text for use on the replace function.

        We cant do a positive lookbehind with the GREP ^.+? but we can find both and only use the second found set in our replace function.

        GREP Find
        “Find, as found set one, one or many characters (shortest match), which is followed by, as found set two, a normal space.”

        GREP Replace
        “Replace with $1, which was the results of the GREP within the first set of parentheses, and a forced line break.”

    • #75966
      Jahrod
      Member

      Is it also possible to find only the last word of a paragraph and move it to a new paragraph?

      For instance:

      ‘Today I want to be in the sun’.

      Should change to:

      ‘Today I want to be in the
      sun’.

    • #75969
      Eugene Tyson
      Member

      \w+.?$

      Change to
      $0

    • #75973
      Justin Sligh
      Member

      Eugene’s GREP find/replace will do the trick.

      Go with the most simple solution based on the text. If you create a complex GREP, you are more likely to find undesired results.

      For the sample text you provided, \w+.?$ is perfect.

      If your text is is more complex with punctuation within a single word (e.g., hyphen, apostrophe), you might need something more complex. Here is an example of a more detailed GREP that takes care of words that have one punctuation mark.

      (?<=\x20)\w+[[:punct:]]?\w+?(?=[[:punct:]]+$)

      “Find one or more word characters followed by a possible punctuation, followed by possibly one or more word characters, which comes after a normal space and before one or more punctuations at the end of a sentence.”

      If your full text is as simple as the sample text that you provided, this excessive.

    • #75980
      Eugene Tyson
      Member

      Yes, I was going to get into that, if there was a word hand-hyphenated then I would do what you have done there.

      It gets trickier with urls too.

    • #76021
      Jahrod
      Member

      Justin and Eugene, thank you so much for your help and explanations. Such a huge help!

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