Grep Find/Replace, multiple formatting applied

Learn / Forums / General InDesign Topics / Grep Find/Replace, multiple formatting applied

Tagged: 

Viewing 11 reply threads
  • Author
    Posts
    • #89146
      Kelli Fleck
      Member

      I am wondering if there is a way for me to replace the word “pKa” with different formatting. For example, I have throughout multiple documents the instance of:

      p (regular lowercase) K (regular uppercase) a (subscripted regular lowercase)

      I want to change it to:

      p (regular lowercase ) K (italics uppercase) a (regular lowercase)

      Is there a way to do this in Find/Replace all, perhaps with GREP? So far I have only seen a way to change for one type of formatting and not multiple.

    • #89150
      Matt Isaac
      Participant

      I’m pretty sure this can only be done with two grep expressions first to change the K: (?<=\<p)K(?=a\>) second to change the a: (?<=\<pK)a\>
      These can be combined into a single shortcut command if applied via script such as:
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "(?<=\\<p)K(?=a\\>)";
      app.changeGrepPreferences.fontStyle = "Italic";
      app.changeGrep();

      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = "(?<=\\<pK)a\\>";
      app.changeGrepPreferences.position = Position.NORMAL;
      app.changeGrep();

      for information on installing and using the script see: https://creativepro.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-or-blog-post.php

    • #89152
      Kelli Fleck
      Member

      Great! Just used those expressions and it did the job. Thank you!!

    • #89153
      Kelli Fleck
      Member

      OH! LOVE that. I am going to create that shortcut. Thank you!

    • #89156
      Kelli Fleck
      Member

      Trying to write one for ml to mL..I got this far:

      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.findWhat = “(?<=\\<m)l\\>”;
      app.changeGrepPreferences.position = Position.NORMAL;
      app.changeGrep();

      Could you help with this one?

      Do you know any good sites for learning the basics in GREP?

    • #89158
      Matt Isaac
      Participant

      https://creativepro.com/resources/grep would be a good starting point for learning GREP.
      The GREP for ml to mL would be a bit simpler than your kPa find:\<ml\>change to:mL

      here are the expressions broken down to hlep you to understand them:
      (?<=):This piece of grep looks behind for anything after the ‘=’ without selecting it (eg. (?<=f)un will select ‘un’ only if it is after ‘f’).
      (?=)does the same for looking ahead (eg. f(?=un), will only select ‘f’ if it is before ‘un’).
      \< and \> looks for word beginnings and endings respectively. (eg \<fun\> will only find ‘fun’ not ‘funny’ or ‘funds’)

    • #89161
      Matt Isaac
      Participant

      https://helpx.adobe.com/indesign/using/find-change.html also has some useful tips.

    • #89162
      Kelli Fleck
      Member

      Oh great! Thank you for all the help. And thank you for the helpful explanation. I am on my way to learning some GREP!

    • #89175

      Have you guys ever used https://rubular.com to test your expressions? I’d recommend it. It’s a fast way to test your expressions.

      In my last job I wrote a lot of code and sometimes figuring out the regular expressions (REGEX) was the hardest part. If you don’t use this tool, what are you guys using? Finding the right tools is important.

    • #89245
      Kelli Fleck
      Member

      Hey I am having trouble with the script for pKa, changing the a. It is because there is a character style applied to it. Any suggestions? It makes the a normal but there was a 1.5pt baseline shift on the subscript character style. So it still has the 1.5pt baseline shift but no subscript.

      Is there a way to add 0pt baseline shift to the code?

      :) Thanks for any help!

    • #89249
      Matt Isaac
      Participant

      Just set app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyles[0] in the script just after the position line.. If the subscript was applied with a character style you will be able to remove that line as setting the character style to none will take out the subsccript anyway.

    • #89250
      Kelli Fleck
      Member

      Perfect!! Works like a charm. Thank you!!

Viewing 11 reply threads
  • You must be logged in to reply to this topic.
>