June 26 2007 • 4:17 PM

Using GREP to Make a Character Lowercase

On another post, Eugene wrote: “All the section numbers within text had to be written as s123, but some wisecrack decided to to put them in as S123.” This was clearly a case in which there was a “regular pattern of text” and therefore for find/change with GREP. But he was having trouble figuring out what GREP codes should be. I found one solution, but it seems like there should be a easier way.

The problem is that the GREP tab of the Find/Change dialog box doesn’t offer a Case Sensitive button. I searched for \<S(\d). That means “at the start of a word, find a capital S followed by a digit). And then I replaced with with s$1 (which should mean a lowercase “s” followed by the digit that was found)

But does this work? No. Because InDesign apparently thinks that case-sensitive is turned off, so it figures that the uppercase S should be replaced by another uppercase character. Grrr.

There are codes in most GREP implementations that let you convert text from upper to lower case (or vice versa), but I can’t get them to work in InDesign.

The solution I found was to copy a lower case “s” to the clipboard, then use this expression in the Change To field: ~C$1 (which pastes the unformatted contents of the clipboard, followed by the digit)

I am definitely no GREP expert, so I’m curious to hear what other folks here might suggest. (Please only post suggestions that you’ve found actually work.)

Here’s another post on GREP by Michael Murphy, though it was written before CS3 came out.

28 Responses discussing this post. Add yours below.

  1. June 26th, 2007 • 4:38 pm • Link

    Brilliant, I’ll use that from now on and save it. I was stressing out earlier on how to do this and when I found my solution i thought I was a genius, that way is a little more elegant, I’ll have to write it down in my little GREP journal that I’ve started. Thanks a million.

    Eugene

  2. Vectorbabe
    June 27th, 2007 • 1:10 am • Link

    I would do it slightly differently.

    In fact, I don’t even need GREP to do it. I could do it in CS2 or before.

    Create an empty character style that I’ll call Tag.

    Search for S^9^9^9
    and apply the Tag style.

    Then search for S with the Tag style and replace with a lowercase s.

    This is the kind of stuff I’ve been doing with wildcards for years.

  3. Matthew Treder
    June 27th, 2007 • 3:34 am • Link

    Vectorbabe has been riding with the wildcard cowpokes who’ve roamed the range, lassoing hacked-up and lopped-off email text and russling it back into shape for lo! these many years.

    Before GREP, some of the most ingenious Word macros for text cleanup ever known were spotted in the wild. To appropriate the sentiments of Samuel Johnson, those early kludges and workarounds were like a dog’s walking on his hind legs: far from elegant, but you were surprised to find them done at all.

  4. June 27th, 2007 • 6:33 am • Link

    Here’s another GREP-cheat, similar to David’s…

    Find what: \

  5. June 27th, 2007 • 6:36 am • Link

    (second try, think the comments don’t like the backslash char??)

    Find What: \
    Change to: ~hs$1

    This works as long as the paragraph style isn’t relying on an End Nested Style Here character.

  6. June 27th, 2007 • 6:37 am • Link

    that should be Find What:
    backslash

  7. June 27th, 2007 • 6:39 am • Link

    ok, last try… (it’s the “greater than” that’s not liked???)

    Find What: \greaterthanS(\d+)

    (replace the greaterthan with the actual symbol)

  8. June 27th, 2007 • 6:40 am • Link

    b.t.w. for what it’s worth, I tried marking up the code with the pre-tag, but that wasn’t appreciated either.

    how did you manage to include the greaterthan char David?

  9. June 27th, 2007 • 8:37 am • Link

    Vectorbabe, thank you for your feedback. It is a good way of doing it, and I wish I had thought of that sooner.

    The purpose of doing it with GREP was to do it with GREP, I thought it was going to be easy to do it, which it probably is.

    What’s not mentioned in the main post above is that I selected the Case Sensitive in the Find Text tab, then searched my GREP in the GREP tab, and only then did the lowercase become active in the replace.

    Also, I couldn’t replace them all, nothing happened when I pressed that button, but I did change them one at a time in a few minutes.

    My main issue was that I didn’t want to apply Character Styles to everything I wanted to change to lowercase, and there was no “Case Sensitive” switch on the GREP dialouge box plus, the “change all “button did nothing with the case sensitive switch on the “FIND” tab selected.

  10. David Blatner
    June 27th, 2007 • 12:59 pm • Link

    Sandee (v.b.): Great non-GREP idea! Wish I had thought of that.

    Cari: Yes, this is going to be an interesting challenge. When you type a lessthan (<) character (which I think you mean… not greater than), WordPress assumes you’re going to type some HTML. I think it should work if you type “& lt ;” (but without the quotes or spaces).

    But as for your replacement code: ~hs$1… You’re adding an end nested style character at the beginning. Wow! Why does that work?

  11. Chris Gosling
    June 27th, 2007 • 1:04 pm • Link

    You could also do a combination GREP/old fashioned find/replace. Not that the Vectobabe solution isn’t good but it just runs into problems if you have number sequences of different lengths. I also don’t like adding character styles just for find/replace purposes.

    What you could do is a GREP search that replaces the uppercase “S” to something that is not found in the document i.e. \ %s%$1 - and then do a normal find/replace when you change %s% to the lowercase s.

    Agreed that this is something that GREP should handle - but it’s a little better than just assigning character styles when not really needed.

  12. Victor Caston
    June 27th, 2007 • 7:10 pm • Link

    That’s too bad — in BBEdit, you can put case transformations in the replace field (\u, \U, \l, \L, \E). Very useful.

  13. June 28th, 2007 • 2:28 am • Link

    David, phew… talk about having a bad ‘comment’ day… couldn’t even get my < and > s right :) :)

    “Change To” doesn’t seem to acknowledge a literal lower case character as a first character.

    The “End Nested Style Here” char, takes up no space and turns into a hidden first character. The literal ‘s’ will then work, as it is no longer the first character

    Even when I tried using Unicode “\x{0073}”, which should be a lower case ‘s’, I ended up with an upper case ‘S”.

    Not sure if this would be a feature or a bug ;)

  14. H.M. Shafiullah
    June 29th, 2007 • 10:24 am • Link

    While working with rule above the text in InDesign CS2, the above rule with offset is placed away from the text frame. I want it to be placed within the (or top of) frame as in QuarkXpress. ANy suggestion please ….

  15. David Blatner
    June 29th, 2007 • 12:55 pm • Link

    H.M., your question doesn’t really have anything to do with this post, I think… but the answer is: CS3 has this feature!

  16. June 30th, 2007 • 5:03 am • Link

    Found another solution, which I think is the best of the GREP solutions… as it requires minimal steps and does not include usage of a hidden character.

    Insert a NUL character first (Unicode Value 0000). This does nothing as far as I am aware. But you can now type that lower case ‘s’ without any problems.

    E.g.

    Find what: \<S(\d+)

    Change to: \x{0000}s$1

  17. June 30th, 2007 • 1:03 pm • Link

    Wahey, that’s good work. Thanks a million.

  18. February 3rd, 2008 • 3:05 pm • Link

    After listening to podcast number 70, I find that there is still no answer to the basic question:

    How do you Search for entire paragraphs of ALL CAPPED text and convert it all to lowercase, sentencecase, or titlecase?

    In many text situations, there are paragraphs occurring throughout text where the writer used ALL CAPS.

    An ideal GREP search would find all uppercase words in the entire paragraph; convert to another case; then move on to find the next randomly-occurring paragraph with the same affliction.

    I can’t get InDesign CS3 to do it, yet. I noticed David began to comment on that aspect in the podcast (70) and then Anne Marie steered the conversation in a different direction.

    This need seems so essential to me, that it leaves me thinking that GREP search would certainly allow for this.

    From what I’ve read, other GREP softwares CAN do this, but InDesign cannot.

    What are others finding on this?

    Mike Witherell in Maryland

  19. Simon Soenens
    April 29th, 2008 • 7:59 am • Link

    Is it at all possible to change a certain character style to lowercase?

  20. S.C.Shaw
    July 12th, 2008 • 5:12 pm • Link

    How do you fix a typo when an uppercase character has been typed between two lowercase characters such as:

    HeLlo

    to this:

    Hello

    I’ve tried all the variations in this blog archive, been able to isolate the uppercase L but cannot figure how to change it to a lowercase L.

    Appreciate any answer.

  21. Eugene
    July 13th, 2008 • 7:23 am • Link

    Mike:

    You can just search for normal text, you won’t need GREP. In the find format section you can just search for BASIC CHARACTER STYLE>CASE>ALL CAPS

    And Replace Format with BASIC CHARACTER STYLE>CASE>NORMAL

    Keep in mind that the caps text refers to sections of the Find Format and Change Format of the find panel.

    Simon:

    There isn’t a way to change a character style to lower case, you would need a script to do this.

    S.C.Shaw

    This grep search will find all uppercase letters in between words, but I can’t think of a way to make them lowercase.

    (?!\b)(\u+)

  22. July 13th, 2008 • 2:03 pm • Link

    To convert a grep to lowercase (and have it work on variable text), you need a script. Here’s a script I recently wrote which uses this technique to convert all caps to small caps:
    http://www.in-tools.com/indesign/scripts/freeware/CapsToSmallCaps.zip.

    To convert an grep to lowercase in your current doc, set your grep in the dialog, and run this script:


    finds = app.documents[0].findGrep();
    for(i=0;i<finds.length;i++){
    finds[i].contents=finds[i].contents.toLowerCase();
    }

    Harbs

  23. David Blatner
    July 14th, 2008 • 6:13 am • Link

    Harbs, that’s a fascinating script! It is very clever to let the user type the GREP into the Find/Change dialog box, then you grab what they typed and use it in your script. Seems to work great.

    Makes me think: It would be so cool if there were a “run this script on All” button inside the Find/Change dialog box. Interesting…

  24. Eugene Tyson
    July 14th, 2008 • 6:17 am • Link

    Or just an action panel would do.

    Find GREP. Run Script. Repeat until done.

    Then of course you could have many more actions saved, removing the need to repeat yourself in InDesign.

    OR, you of course could have the option for lowercase in the field of cases in the change format dialog box.

  25. S.C.Shaw
    July 15th, 2008 • 4:21 am • Link

    Thanks guys for all the input.

    Being new to this scripting angle, is the displayed example script (not the smallcaps zipped one) from Harbs a Java script?

    If so I simple must copy and paste his text into BBEdit and save the file with an extension of .jsx and move it into my IDCS3 scripts folder?

  26. S.C.Shaw
    July 15th, 2008 • 4:41 am • Link

    Figured it out.

    Double-clicked on Harbs CapsToSmallCaps script which opened my ExtendScript Toolkit 2 app.

    Copied/pasted below text

    finds = app.documents[0].findGrep();
    for(i=0;i<finds.length;i++){
    finds[i].contents=finds[i].contents.toLowerCase();
    }

    into a new script window, saved new script into IDCS3 scripts folder, did the GREP search, ran the script, PERFECT!!!
    GENIUS!
    PRICELESS!!

    Thank you again.

  27. July 15th, 2008 • 4:30 pm • Link

    Just stopped back in now…

    To run the above script on all open documents, just change the first line to:
    finds = app.findGrep();

Subscribe to the Discussion

Get the ongoing discussion surrounding "Using GREP to Make a Character Lowercase" delivered to you. Click here to subscribe via RSS.

Leave a Reply

You can use limited HTML tags, such as <em></em> for emphasis/italics and <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

InDesignSecrets reserves the right to edit and/or remove posts and comments.