# RegEx and Back References

**URL:** https://community.mp3tag.de/t/regex-and-back-references/4880
**Category:** Support
**Created:** [May 15, 2007, 2:39pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880 "2007-05-15T14:39:15Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![JFord](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jford/32/4977_2.png) [@JFord](https://community.mp3tag.de/u/JFord)
#### Post date: [May 15, 2007, 2:39pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/1 "2007-05-15T14:39:15Z")

</div>

I am a novice at regular expressions, and MP3Tag is where I am cutting my teeth. I still find that I am wrong many more times than I am right and I don't want to use this forum to teach me how to create them. I am hoping that I have found an issue and it's not my misunderstanding.

I am attempting to parse a Comment field with the text "Before T3 After". However "T3" may be T1, T2, T3, T4, or T5. I figure that in RegEx the way you best describe that is "T[1-5]". Assuming I am correct I would then think that

> $RegExp(%Comment%,^(._)T([1-5])(._)$,$1/$2/$3)

would return

> "Before /3/ After"

but instead I get

> "Before //3 After"

QUESTION #1: Why didn't that example work? Do character classes not return references?

During the next hour as I throw anything I can find at this problem (and there are so many things in RegEx) I try

> $RegExp(%Comment%,^(._)T([1-5]+)(._)$,$1/$2/$3)

which I still think should work (but I still might be wrong about that), it returns an error (or two):

> REGEXP ERROR: Regular expression  
> Invalid preceding regular expression

QUESTION #2: Why did that expression return an error?

Finally, I came up with

> $RegExp(%Comment%,^(._)T(\d)(._)$,$1/$2/$3)

which works well enough for this time (since there will never be a T0 or T6-T9, but I don't care if there is then matching to it is OK)

QUESTION #3: Is there a better way to do what I want restricting to T[1-5]?

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [May 15, 2007, 3:03pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/2 "2007-05-15T15:03:55Z")

</div>

You must write '[' and ']' when you use scripting.

---

<div class="post-metadata">

### Author: ![JFord](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jford/32/4977_2.png) [@JFord](https://community.mp3tag.de/u/JFord)
#### Post date: [May 15, 2007, 4:05pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/3 "2007-05-15T16:05:07Z")

</div>

I don't understand.

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [May 15, 2007, 8:02pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/4 "2007-05-15T20:02:54Z")

</div>

$RegExp(%Comment%,^(._)T('['1-5']')(._)$,$1/$2/$3)

---

<div class="post-metadata">

### Author: ![JFord](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jford/32/4977_2.png) [@JFord](https://community.mp3tag.de/u/JFord)
#### Post date: [October 29, 2007, 11:32pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/5 "2007-10-29T23:32:56Z")

</div>

I am back, and now I believe I might have the opposite problem. This time I need to find the square bracket literals ("[" and]") in a string (actually isolate what's between square brackets). So, I would think that

- $regexp(%Title%,^(._)[(._)],$2)

would do the trick, but that just returns an error. I assume it's something funny about the square bracket character again. Is that right?

Also, are their any other differences between vanilla Regular Expressions and the implementation within the $RegExp function?

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [November 4, 2007, 7:10pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/6 "2007-11-04T19:10:04Z")

</div>

The problem is that we are using two different languages within the $regexp function - title formatting language and regular expressions. Unfortunately, I have no means of seamless language composition at the moment.

The square brackets are special characters in the title formatting language and you have to quote them with ' if you want them to be displayed. As a consequence the call to the $regexp function should look like this

$regexp(%Title%,^(.\*)\'['(.\*)\']',$2)

---

<div class="post-metadata">

### Author: ![JFord](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jford/32/4977_2.png) [@JFord](https://community.mp3tag.de/u/JFord)
#### Post date: [November 7, 2007, 3:41pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/7 "2007-11-07T15:41:31Z")

</div>

I see how it works! Thanks, that finally makes sense:

'[' starts a character class definition  
'[' is the literal open square bracket character

I actually solved the problem using escape sequences (\x5b for '[' and \x5d for ']')  
$regexp(%Title%,^(.+)(\x5b)(.+)(\x5d),$3)

Is the square bracket the only issue within the RegExp function?

---

<div class="post-metadata">

### Author: ![Florian](https://community.mp3tag.de/user_avatar/community.mp3tag.de/florian/32/5759_2.png) [@Florian](https://community.mp3tag.de/u/Florian)
#### Post date: [November 7, 2007, 6:13pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/8 "2007-11-07T18:13:31Z")

</div>

> [@JFord](#):
>
> Is the square bracket the only issue within the RegExp function?

To my current knowledge: yes.

---

<div class="post-metadata">

### Author: ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)
#### Post date: [February 9, 2026, 12:20pm UTC](https://community.mp3tag.de/t/regex-and-back-references/4880/9 "2026-02-09T12:20:17Z")

</div>


