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

Anyone willing to correct a GREP expression

Tagged: ,

Return to Member Forum

  • Author
    Posts
    • #66448

      I have a GREP expression that almost works correctly but not quite. If anyone is willing to take a look at it, please comment and I’ll post the code and the problem. Thanks.

    • #66455
      Matt Mayerchak
      Participant

      I am willing to take a shot at it. I also know of a GREP group on Facebook that may be of help . . . so, if your GREP is over my head, I’ll ask the group if they would like a new member!

      Post away!

    • #66457

      I convert files for printed books into ebooks and need a code to automatically add anchors and links to manuscripts that have hundreds and hundreds of footnotes. So far GREP has been able to do a good job, except this expression that’s intended to create an anchor (a id=”#”) based on the footnote number. It will only return the first digit of the footnote, so that footnotes 10 -19 return as “1” and 20-29 return as “2”, etc. I’m just not savvy enough to spot the error.
      SEARCH EXPRESSION: (<span class=”xfootnote”>\[<a href=”#r)(\d+?)
      REPLACE EXPRESSION: $1$2
      I apologize in advance for being so stupid. Thanks!

    • #66458
      Matt Mayerchak
      Participant

      What is the question mark for in the 2nd set of parens? It should work with just (\d+).

    • #66460

      The question mark is indeed the cause of the malfunctioning.

      By default, GREP is Greedy: the operators ?, +, and * will match as much as possible (right until the rest ofthe expression puts a stop on it). If an expression ends with one of these, it will continue matching as much as possible until the repeated expression no longer matches. A question mark changes this: after a ?, +, or * will make it match as *little* as possible.

      So what happens on a string of digits, such as “abc123efg”? Using “\d+”, it will match the entire set of digits “123” — as long as possible. With the added question mark “\d+?” it will match *as little as possible* (while still being a valid expression), and so just “1” is already enough.

      In the OP’s case this happens because the Repeat modifier is at the very end of the GREP expression. If there is anything in the epression *after* this Repeat modifier, such as “\d+?” (digits but with a next lowercase letter), then GREP has no other option than to match “123e”, because the shortest *possible* match MUST include all digits.

      • #66461

        Thank you both. That’s a part of GREP that I’m not familiar with. Can’t tell you how much this helps.

    • #67530

      I am a new comer in grep. I tried to change with GREP find & replace function the words within brackets to a character style (without changing the brackets itself) with the following grep expression: (?<=\(-.*?(?=\))

      Starting the search it does not found any such string though there are plenty in the text. What is wrong with the expression.

      Similar task would be to apply a character tag for a words begins with opening parenthesis, the word to be changed until a hyphen (the hyphen should not be changed).

      Thanks a lot in before hand

      Laszlo

    • #67531
      David Blatner
      Keymaster

      Laszlo: I think the code you want is (?<=\().+?(?=\))

      • #67537

        Dear David,

        Thanks a lot. How to apply a character tag for a string starting with an opening parenthesis, following the word to be changed terminated by a hyphen (the parenthesis and the hyphen should not be changed).

        Have a nice day

        Laszlo

    • #67538
      David Blatner
      Keymaster

      Laszlo: I think you would need to apply it with two different GREP Styles — one for the first half and one for the second half.

    • #67542
      Matt Mayerchak
      Participant

      If you’re using find/change, not a GREP style. . . it sounds to me like you just want to use a positive lookbehind for an opening paren, followed by a string of Word characters (one or more), followed by a positive lookahead for a hyphen. If that’s correct, then you could apply the character style to what you find.

      If that is the case, you can do all of this via the dropdown menus in the find/change dialog box – see if you can figure it out! If you get stuck, write back and I or someone else can post the correct grep code. But I bet you can figure it out from the menus . . .

      Did I miss something?

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