Un-stripping a string of characters

Hi folks,

I want to turn a string of 16 digits, such as 1234567891234567

into this: 1234-5678.9-12345.67

The actual digits will change each time, but the punctuation pattern stays the same. Can it be done using only KM actions or regular expressions? I’m a beginner at both, but I have no understanding of Applescript or any other scripting language.

You can use regular expressions for this with a search and replace variable or clipboard to change the text:

Search for: (\d\d\d\d)(\d\d\d\d)(\d)(\d\d\d\d\d)(\d\d)

This isn’t the most elegant way to write it, but is the easiest to understand. Basically looks for a number and groups them into the pattern length you need.

Replace with: $1-$2.$3-$4.$5

Replaces the matched numbers above with the same captured digits but with the special characters inserted now.

You could set up a KM macro:

  1. Keystroke ⌘-X (to cut the text into the clipboard)
  2. Search and replace the clipboard using the regular expression as above
  3. Keystroke ⌘-V (to paste the next text back into place)
1 Like

Hey There,

Easy enough.





Using enumeration in the regular expression makes it easier to tell at a glance how many digits are in each segment.


1 Like

Thanks, Michael and ccs. That works perfectly. I had kind of figured out how to find the groups of digits, but I was stuck on how to do the replacement – I didn’t know you could refer back to each group with a $ or backslash. Appreciate the examples!

–Bill G

Whups. I see I used backslashes to refer to the capture groups. This is proper syntax for BBEdit and TextWrangler but not KM. I’ve corrected my oversight in the forum post.

Half the fun of regex is keeping track of all the various flavors for each app.