Bitz
Bitz

Reputation: 1148

All purpose bbcode regex

So currently, my regex:

\*\*(.+?)\*\*

Returns ***400 FAVES! Thank you so much for your love and support!**

When I put in [size=16]***400 FAVES! Thank you so much for your love and support!***[/size]

Note that there is an additional * that is omitted at the end.

Can someone please tell me why the that is happening and what the best solution is?

Upvotes: 0

Views: 24

Answers (2)

Piyush
Piyush

Reputation: 1162

If you are sure that your capture string contains exactly 3 stars at the beginning and end, you can use \*{3}(.+?)\*{3} in that case. Otherwise, a greedy expression like \*(.+)\* should work as long there is a star at the end and the beginning.

Output : ***400 FAVES! Thank you so much for your love and support!***

The reason it returned 2 stars at the end in your case is non-greedy (.+?) part of the regex that matches as few times as possible.

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89567

That is the normal behaviour of a non-greedy quantifier (that takes the less as possible).

You can solve the problem including optional asterisks in your capture group but this time with a greedy quantifier:

\*\*(.+?\**)\*\*

demo

Upvotes: 1

Related Questions