# Strange regex engine behavior with a lazy star token

**URL:** https://community.mp3tag.de/t/strange-regex-engine-behavior-with-a-lazy-star-token/10558
**Category:** No Bugs
**Created:** [June 18, 2010, 6:09pm UTC](https://community.mp3tag.de/t/strange-regex-engine-behavior-with-a-lazy-star-token/10558 "2010-06-18T18:09:37Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![ptrk.mj](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ptrk.mj/32/300_2.png) [@ptrk.mj](https://community.mp3tag.de/u/ptrk.mj)
#### Post date: [April 24, 2011, 5:51pm UTC](https://community.mp3tag.de/t/strange-regex-engine-behavior-with-a-lazy-star-token/10558/3 "2011-04-24T17:51:33Z")

</div>

I decided to bring the topic back to life.

> [@](#):
>
> Yes, this is rather confusing ... **one single character should be replaced by one single character** , and the result is a string of three characters!

As I explained in the first post regex engines apart from matching at each character offset (in our one-character example at 'offset 0') also try to match a pattern at an offset behind the last character (i.e. match a pattern against void at the end of the string) (in our case 'offset 1').

Put $regexp(a,.\*?,+) in any regex tool and you get +a+

Two zero-width matches (at offsets described above) are replaced with + character. That's 100% correct! Yet, Mp3tag outputs +++

1. 

> [@](#):
>
> $regexp('a','.\*','+') will give _two_ plus characters.

**Correct!**

Match 1: a  
Match 2: {void at the end of the string}

1. 

> [@](#):
>
> $regexp('a','^.\*?','+') will give _two plus_ characters.

**Wrong!**

This regex should produce +a

Explanation: There is only one zero-width (due to laziness of the star) match at 'offset 0'. Match at 'offset 1' fails (due to ^ anchor).

1. 

> [@](#):
>
> $regexp('a','.\*$','+') will give _two plus_ characters.

**Correct!**

It matches the same way as 1. did. $ anchor doesn't change anything here.

1. 

> [@](#):
>
> $regexp('a','.\*?$','+') will give _two plus_ characters.

**Correct!**

Again, same matches as in 1. and 3.  
.\*? token will be forced to expand it's match to letter a by $ anchor. After that, there's another match at the end of the string.

1. 

> [@](#):
>
> $regexp('a','^.\*?$','+') will give a single plus character as the result.

**Correct!**

Match 1: a

Single one-character long match at 'offset 0'.  
$ anchor makes the .\*? token expand it's match to cover whole string.  
^ anchor assures that pattern cannot be matched after the string ('offset 1').

What we learn from above:

- The problem occurs only when using \*? (lazy star) token.
- Using $ anchor prevents the bug from appearing

Also:

- The bug will show up when using character token in regex that matches the character in the string (in our case it's the dot matching 'a')

Other examples:  
$regexp(b,b\*?,+)  
$regexp(9,\d\*?,+)  
etc.

---

_[View the full topic](https://community.mp3tag.de/t/strange-regex-engine-behavior-with-a-lazy-star-token/10558)._
