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

Searching and replacing using GREP – excluding found characters

Return to Member Forum

  • Author
    Posts
    • #62431
      Anonymous
      Inactive

      Hello chaps,

      A GREP query here, I am trying to parse the expression:

      Find any character (that has a certain attribute) that is followed by a soft return

      Change the soft return to a hard return, but leave the found character unchanged.

      Also:

      Find any letter m that is followed by any digit.

      Change the digit to a superscript, but leave the found m unaltered.

      The common factor as you have seen is that while I can work out how to find things with GREP I am not surgical enough, the GREP expression I might put together would be too simple and would also select the character I need to leave unchanged.

      Can anyone help?

      Thanks

    • #62432
      Anonymous
      Inactive

      I should clarify that the soft return in the first example may or may not share the attribute of the found character.

      So I may attempt n(?=.) to find the soft returns and specify an attribute such as [Bold], but some of the returns will be bold and some may be set to Light.

      So perhaps I might better describe what I need to parse:

      Find a soft return (of any attribute) that follows any character (which has a Bold attribute) and precedes any character (which has a Light attribute)

      Change the soft return to a hard return

    • #62434
      Anonymous
      Inactive

      So thinking about it, I had the expression the wrong way around for the second query.

      I am looking for the m but I should be looking for the digit.

      I have been reading Michael Murphy's GREP In InDesign but I must have an old copy as I'm sure he has mixed up Positive Lookahead and Positive Lookbehind which has been throwing me a proper curve.

      With a bit of luck I can crack the other one before I go home, sorry for this stream-of-consciousness forum post.

    • #62435
      Anonymous
      Inactive

      Final post before I go and mix myself a large drink

      I have come up with d(?<=m) to find my second query items – but it doesnt work.

      A very large drink, I think.

    • #62443

      Move the lookbehind before the digit :)

      InDesign does not tell you if your GREP is invalid — it just issues a “not found”, even when you enter nonsense. A mistake in my opinion … But this expression is valid, but it just logically cannot find anything. Imagine each character that does match moves an internal cursor one character ahead. In your case, a digit is found, and the next command checks to the left of the cursor if there is an “m”. Well, there can't be, because there will always be a digit there …

    • #62444

      As for your first question: no, you cannot selectively search for two characters where one must have a certain formatting and the other i have.

      You cannot enter formatting specifications in the Find Formatting field for parts of your find text — it's always going to apply to the entire find string, including any lookbehinds or lookaheads. See Mike Rankin's A Little Grep Gripe: https://creativepro.com/a-l&#8230;..-gripe.php

    • #62446
      Anonymous
      Inactive

      Thanks Jongware.

      Just to clarify, whereas I assumed my query was:

      Please select any digit that is preceded by an 'm'

      I in fact was telling InDesign to highlight all the digits in my document, then check to the left for an m… but the action of selecting the digit automatically precluded the possibility of there being anything other than the found digit… I could have pondered for a million years and not worked that one out.

      Would I be more accurate in thinking of all GREP queries as a kind of procedural thing, so my query in fact stated to InDesign:

      Select the first digit in the document. Check the immediately preceding character for an m. If no match, continue.

      If that's true then that little wrinkle will be very useful to keep in mind as I scale the first foothills of the Everest of regular expressions.

    • #62447

      This ought to work:

      (?<=m)d

      You can think of this procedural method: first, ignore “lookbehind” and “lookahead” commands, and search for the actual text

      md

      and then exclude the “lookbehind/lookahead” parts from the selection.

      It doesn't really work as described, because you can also rewrite your first attempt to

      d(?<=md)

      … in fact skipping back over the “just-encountered-digit” in the lookbehind …

      Hope this clarifies it a bit!

    • #62451
      Anonymous
      Inactive

      Thanks again, I really would never had worked that out from gazing at the screen and weeping, which has been my preferred method of GREP problem solving up until now.

      And knowing that GREP doesnt account for formatting in more than a superficial way is helpful too, as it will stop me from trying impossible things and concentrate on finding another path.

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