ID:2928592
 
Not a bug
BYOND Version:515
Operating System:Windows 11 Pro 64-bit
Web Browser:
Applies to:Dream Seeker
Status: Not a bug

This is not a bug. It may be an incorrect use of syntax or a limitation in the software. For further discussion on the matter, please consult the BYOND forums.
Descriptive Problem Summary:
When using backrefrences for regexes along with matching amounts, dream demon runtimes with the following error
runtime error: Regex fail: * + {} operand could be empty

Regex to test directly in other languages or regex101: ((\n)\2{2})\2+

Numbered Steps to Reproduce Problem:
-> create the regex
-> run a valid text through the regex

Code Snippet (if applicable) to Reproduce Problem:
var/a = "text\n\n\n\ntext"
world.log << a
var/regex/reg = regex("((\\n)\\2{2})\\2+")
world.log << replacetext(a, reg, "$1")


Expected Results:
text\n\n\ntext

Actual Results:
runtime error: Regex fail: * + {} operand could be empty

Does the problem occur:
Every time? Or how often? yes
In other games? only tested on ss13
In other user accounts? server side
On other computers? server side

When does the problem NOT occur?
When not using backrefrences along with {} like in the example above

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? (Visit http://www.byond.com/download/build to download old versions for testing.)
Did not test it on earlier versions, yet

Workarounds:
writing a horrible regex...

You're using \2 inside the 2nd group itself. I don't see how that would be valid in any regex engine.

I know some engines do allow \2 to be used before the 2nd group, which BYOND also doesn't support, but being used inside the group itself makes no sense; that'd be a recursion.
Lummox JR resolved issue (Not a bug)
https://regex101.com/r/BbM4M4/1

Works fine on regex101 and in java script as I've implemented it on the TGUI side. The goal is to always have the first capture group $1 to contain 3 new lines and match everything above to regex replace and keep the maximum at 3 successive new lines at all times. But if not supported by Byond, I'll just keep it on the TGUI side.
If your goal is to limit consecutive newlines to 3, then wouldn't replacing regex(@"\n{4,}","g") with "\n\n\n" be the correct choice?

I'm baffled that \2 can work inside of its own capture group in any engine. The logic of that doesn't make sense to me.
When wanting to only limit new lines, yes, but if wanting to extend it to more types, it gets quickly ugly. In general you could even limit every existing character to just a maximum of 3 occurrences for example by replacing the \n with just a .

Then the capture group would make sure that the replacement matches the matched characters on replacement
In response to Lummox JR
Lummox JR wrote:

I'm baffled that \2 can work inside of its own capture group in any engine. The logic of that doesn't make sense to me.

That's because it's not it's inside its own capture group. The inner most group is group 2, while the outer most is group 1. A simpler expression that causes this is @"(anything)\1{42}"

From what I can tell so far, the issue is always caused by a backreference being put in front of a {}

Update: tested it with * and + since those were mentioned in the error message, they also crash the regex.
Ohhh, that's right. I forgot $1 was the outer group. I believe the regex engine struggles with situations where the previous group hasn't yet closed.