How to create capture groups in order to rearrange filename components with Regex

634 views
Skip to first unread message

C_Campbell

unread,
Apr 20, 2015, 11:30:32 AM4/20/15
to textwr...@googlegroups.com

Hello, 

I've used the excellent grep search/replace feature in TextWrangler fairly extensively, but don't yet understand how to use capture groups that can be treated as a variable, so as to rearrange the components inside a filename. If someone could assist by showing how to set up the example I'm including, I'll be able to extrapolate this to additional cases.

Here I simply need to exchange the tape number (57-04), with the segment number (seg1). Once I understand how to do us, I will use "A Better Finder Renamer" in OS X to regularize several thousand filenames in a complex audio archive. Regular expression support in this app is based on the RegexKitLite framework, which uses the regular expression engine from the ICU library in OS X. 

Sample filename: 1962-10-22-CH_seg1_57-04.aif

Desired result: 1962-10-22-CH_57-04_seg1.aif

Variations: the numeric component at the end of the filename (57-04), ranges from 1-00 to 999-10.

Note: I tried to post this question on stackoverflow this evening, but got slammed instead of assistance!

Thanks in advance!

—————

Christopher Campbell

Steve

unread,
Apr 20, 2015, 11:48:53 AM4/20/15
to textwr...@googlegroups.com
I don't know about treating capture groups as variables, but with your example:

Search:
    (_seg\d+)(_(?:\d\d\d?|[1-9])-(?:10|0\d))

Replace:
    \2\1

You can see this in action here: https://regex101.com/r/rG7cO3/1

Unless, of course, I misunderstood something. (And if you want to talk to anyone "live", you can join us in chat either directly at irc.freenode.net, channel #regex, or click on the "IRC" link at the top of that regex101.com page, which will let you chat through your browser.)

-Steve

Christopher Stone

unread,
Apr 20, 2015, 5:20:47 PM4/20/15
to TextWrangler-Talk
On Apr 19, 2015, at 21:29, C_Campbell <cbc...@gmail.com> wrote:
> but don't yet understand how to use capture groups that can be treated as a variable
______________________________________________________________________

Hey Christopher,

A capture group is a grep pattern contained in parentheses.

(\w+)(.+)

This pattern has 2 capture groups.

In TextWrangler and BBEdit they are referred to with a backslash and a number:

\1 == Capture 1
\2 == Capture 2

& == Entire found pattern

Other flavors of regex use different syntax for captures. Perl for instance:

$1
$2
$&

* There are special parentheses patterns that do NOT capture.

The online regex analyzer Steve provided is a good learning tool, because you can visualize your pattern in real time as you write it.

There are apps that do this on the Mac and iOS as well.

A few I have:

Patterns (Mac App-Store)
RegExRX (http://www.mactechnologies.com/index.php?page=downloads)
RegEx Knife (iOS App-Store)

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages