Any way to get a regex working in a report format using string.contains?

Specifically, I have a working report that looks like this:

"( <b>" + result + "</b> ) " + PlayerName + " rolled " + nDice + (nDice == 1 ? " die" : " dice") + ". Result: " + ((result.contains("1")) ? "<b>SUCCESS" : (result.contains("2")) ? "<b>SUCCESS" : (result.contains("3")) ? "<b><span style=color:#DB0000;>FAILURE" : (result.contains("4")) ? "<b><span style=color:#DB0000;>FAILURE" : "<b><span style=color:#DB0000;>CATASTROPHIC FAILURE")

The question is how can I combine something like ((result.contains("1")) ? "<b>SUCCESS" : (result.contains("2")) ? "<b>SUCCESS" : in a cleaner way, with one less ? : condition.

Answered on Discord, but I’ll copy it (slightly edited for clarity) here for posterity:

You can use string.matches() instead; it takes a regex expression. So, something like: result.matches(".*(1|2).*").

That should match any string that starts with any number of any characters (including none) followed by a “1” or “2” followed by any number of any characters.

FYI, if you need to match a multi-character string, it would be result.matches(".*(string1)|(string2).*"); note that each side of the bar needs to be in parentheses in that case.

1 Like