is now part of CreativePro.com!

Swap Names Around with GREP and Find/Change

19

Joe wrote:

I have about two hundred text boxes which are captions of student pictures produced by Bob Stucky’s contact sheet script. The captions are actually the file name in this format: LastName_FirstName.jpg. I need to change to FirstName (space) LastName (no file extension).

Sounds like a job for super GREP! Try this: Open the Find/Change dialog box and switch to the GREP tab (this is in CS3 and later). Type this into the Find what field:

^(\w+)_(\w+)\.jpg

Type this into the Change to field:

$2 $1

Now click Change All. That should do it!

Here’s how to decode the grep codes:

  • ^ means “beginning of paragraph”
  • (\w+) means “remember what the first group of one or more word characters are”
  • _ is literal — that is, it’s just looking for an underscore character
  • \. means a period/dot (you have to “escape” that character with the backslash before it)
  • .jpg is also just literal
  • $2 $1 means “take the second word group you found (the stuff in parentheses), then type a space, then type the first word group you found”

There, that wasn’t so hard, was it?

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • For Mac OS X users :

    if you need to list all the files on a Finder’s folder, just :
    – select the files
    – Edit > Copy or Cmd-C
    – Open the app TextEditor
    – Cmd-Shift-T to switch to Plain Text formating or Format > Make Plain Text
    – Edit > Paste or Cmd-V : there you are, the list is there.

  • Eugene emailed me offlist with a very good point: What if the names include non-word characters (characters that the \w won’t pick up), such as “Joe O’Hara” or “Jason Briggs-Jones”. Good point! In that case, perhaps the find code should be:
    ^(.+)_(.+)\.jpg
    or
    ^([-'\w]+)_([-'\w]+)\.jpg

    (The first uses the dot instead of \w, which lets InDesign find any character at all. Seems kind of extreme to me, so the second one limits it to just hyphens, apostrophes, and word characters. But both seem to work.)

  • You can also use :
    ^(\S+)_(\S+)\.jpg

    \S looks for everything except a space

  • Eugene says:

    Well now that the morning has passed and I’ve thought about it, filenames wouldn’t usually have apostrophes, but could have hyphens.

    So anyway, I like the second one there David, it’s a good one.

    I don’t know how to use the [[:punct]] posix in GREP, so I didn’t really know how to search for punctuation.

    I can’t get the second one to work though? Not sure why.

    This one worked for “O’Hare_John-Joe.jpg”

    ^(\w+[-‘\w]\w+)_(\w+[-‘]\w+)\.jpg

    But I guess you can’t get all possible variables for names in there – can you?

  • Something’s wrong : the antislash disappeared
    the good regex is :
    ^(\S+)_(\S+)\.jpg

    [[:punct:]] matches any ponctuation :
    ^([[:punct:]\w]+)_([[:punct:]\w]+)\.jpg

    The first regex is better.

  • I’m not sure why the backslash (or “antislash”) is not working sometimes in these comments. It looks like you need to type it two times in a row to make it appear. I guess our CMS strips it out if you only type it once. I’ve edited your posts, Laurent.

  • Bob Bringhurst says:

    What about names like “de la Vega” or “Wilcox III”? Since the underscore character is so rare, I’d go with this:

    ^(.+)_(.+)\.jpg

    • Cherelyn Gillson says:

      Thanks Bob. I have been trying to get a bulletproof GREP query to change
      Lastname, Firstname to Firstname Lastname for years now. I could get most things to work but when it came to double-barrelled first names or surnames that weren’t hyphenated but just had a space I could never get it to work. I used yours and replaced the _ with \,\s as that is how our captions and named lists generally come in.It worked perfectly and I can’t thank you enough. You will save me hours of work.

  • Eugene says:

    Hi Bob, that’s what I said in post 2, I emailed David about the hole in the grep code for names.

    I pretty much thought the code was solid, but David thinks it’s extreme.

    Seems to be hot topic though :)

  • Harbs says:

    I’d go for this GREP (there’s much less backtracking) :

    ^([^_])_(.+)\\.jpg

  • Roland says:

    I’d give «(?).(?)» a go, but that’s just because it looks way cuter than anything you guys came up with ;)

    On a more serious note: if you work on a copy to run the GREP, there’s no reason not to use as rigorous a replace as possible. I wouldn’t want to end up with a name screwed up because I was too cautious.

  • Ross says:

    Hi guys, an amazing script… Why do the capital letters get removed when I do the GREP command? The file names are like this to begin with

    Surname_Firstname.jpg

    then get changed to

    first name surname

    Should I just select all and hit make title case?

    I’m doing a yearbook for 1500 kids so something that does it automatically would be good!

    • Ross: Not sure why it would change. Shouldn’t! It’s actually hard to get GREP to change case in InDesign.

      • Bryan Bradley says:

        David, first off your work is appreciated.
        I’ve tremendously improved my InDesign skills from your post/Pod cast, etc. my question is if GREP isn’t the best with case changes are you aware of any solution for example when the name starts with a particle (Mac, Mc, M, O) are always capitalized; others (L’, Van) are usually capitalized; still others often are not (d’, de, di, von) Is there anything out there that I can use to help with this? character style? script ? Thanks in Advance.
        B – rye

      • Bryan: This might be better handled in the forums (as your message gets a little lost in this long thread of comments). But the quick version is is that you simply need to come up with a grep expression that looks for all those different variations. I say “simply” a little sarcastically, as it can be tricky to write a single expression that finds all those variations to people’s names.

  • James Cline says:

    I can not get any of these GREP scripts to work,
    I am using a Mac and indesign cc 2014
    I can remove and replace one thing at a time but not all at once.
    I have tried 100 different ways even looking up the code.
    I am working on a yearbook. here are the file names I am trying to change to just first name last name.
    Adams_Tyler2.JPG

    • @James: Are you using the GREP tab? Did you set the scope properly (either Story or whole Document)? Are you trying to get rid of that number 2? You might try searching for (\w+?)_([\l\u]+?)\d?.(JPG|jpg|PNG|png) and replacing with $2 $1 (or some of the codes above)

      • James Cline says:

        Thank you so much David. It works but only on file names with one digit in it. The file names I am working with have up to 3 numbers. Adams_Tyler112.JPG the number being a sequence number the picture was taken in. I am searching for code to add to the formula you gave me. Again thank you so much. You are truly a God sent.

      • James: \d means “a number” and ? means “zero or one of them.” Replace the ? with a * (asterisk) and it means “zero or more times” (you can find these in the Repeat submenu, in the little triangle pop-up menu next to the Find What field in the GREP tab of the find/change dialog box)

  • >