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 to remove footnote numbers in text

Return to Member Forum

  • Author
    Posts
    • #1186762
      Michael Hamilton
      Participant

      So I have a huge document that has footnotes that I do not need. There are numbers in the text that must stay but the footnote numbers need to come out. They always occur before a letter, without a space. (like this: …at the end. 4Then you make your way…) I can find the numbers with: \d[[:alpha:]] or \d[\u] but I don’t know what to put in the “Change to:” in order to delete the number and preserve the first letter of the word. Also the footnote numbers are not superscripted or styled so I can’t search that way. Please help!!!

    • #14324097
      Michael Hamilton
      Participant

      Thanks to all who help!

    • #14324096

      \d+(?=\u)

      where is a word break – there is a “word character” at its right, so there should not be a word character to its left –, \d+ is any sequence of digits, and the final part is a lookahead ensuring the next character is an uppercase.

      You can replace it with nothing.

    • #14324095
      Kal Starkis
      Member

      Yep, I was about to say that a positive lookahead is what you need… This means that the letter needs to be there, but it won’t be included in the matched text.

    • #14324092
      Michael Hamilton
      Participant

      Thank you so much, it worked!
      I noticed that I have other numbers just after letters and punctuation. Any code for this?
      Example: “…the satellite.3 The space between…” I have other numbers that need to stay unaffected like this: “5 The point of the…”
      The numbers I need to keep all have an end paragraph before them and a space after them. Everything else must go.
      Thanks again for your help!!!

    • #14324085
      Kal Starkis
      Member

      Michael, try defining your problem in words like this… I’m looking for a number that occurs AFTER a character that is NOT a whitespace, and BEFORE a whitespace. From there, you can refer to a regex cheatsheet (https://www.rexegg.com/regex-quickstart.html) or even use InDesign’s built-in cheatsheet (the little dropdown menu to the right of the GREP search field) to convert your sentence into regex.

      What you’re looking for occurs ‘after’ something. This indicates a positive lookbehind—in other words, something must occur BEFORE we start matching any text. A positive lookbehind looks like this: (?<=…)

      One way to represent ‘a character that is NOT a whitespace’ is by \S. So the positive lookbehind now looks like this: (?<=\S)

      ‘a number’ is, as Mr Jongware pointed out, simply a string of digits: \d+

      What we’re looking for occurs ‘BEFORE’ something. This indicates a positive lookahead—in other words, something must occur AFTER our match. A positive lookahead, as Jongware pointed out, looks like this: (?=…). A whitespace is represented by , so our positive lookahead becomes (?=).

      Put it all together, and we have:
      (?<=\S)\d+(?=)

      When you test this, you might find a problem. It matches the ‘234’ in ‘1234’. That’s because the digit ‘1’ also satisfies the criteria ‘NOT a whitespace’. So how would you fix this? You could change your sentence to ‘NOT a whitespace or a digit’. ‘OR’ logic can start getting confusing, but regex has a really nice way of matching a set of possible characters. You just put all the options in square brackets. For example, [abc] will match a, b, OR c. [\d] will match a whitespace OR a digit. But we want to match everything that is NOT one of these characters. We can do that by inserting ^ after the opening bracket like this: [^\d]. Here, the ^ means ‘NOT’.

      So now our improved regex looks like this:
      (?<=[^\d])\d+(?=)

      We could have done the same thing with a NEGATIVE lookbehind. The ! in a negative lookbehind also means ‘NOT’:
      (?<![\d])\d+(?=)

      If you’re an absolute beginner with regex, I wrote a very basic tutorial which you might find useful: https://inkwire.app/articles/getting-started-with-regular-expressions.html

      There are some links at the bottom of that page for more useful resources.

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