0

I'm using the regular expression

"\x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]"

to strip ANSI control codes out of a string. This works just fine with sed, but if I feed that string to a QRegularExpression it fails to match the control characters. I've tried using the QRegularExpression::escape() function; tried escaping just the leading '\x1B'; tried using '\e' in its place, all to no avail.

SixDegrees
  • 781
  • 1
  • 8
  • 19

2 Answers2

1

Try this

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    const char data[] = "some \x1b[31;1mtext";

    QRegularExpression re("\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]");
    QRegularExpressionMatch match = re.match(data);

    if (!re.isValid()) {
        std::cout << "not valid" << std::endl;
    }
    if (match.hasMatch()) {
        std::cout << "matched" << std::endl;
    }

    return 0;
}

You should escape symbols like [ ("\\["). You can use both "\x1b" and "\\x1b" in QRegularExpression (in the first case it'll be just 0x1b symbol).

Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
0

You can use a raw string literal and forget about the problems with escaping regex escapes:

auto re = QRegularExpression { R"\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mK]" };

Here, each \ is treated as a literal backslash.

Also note that [m|K] matches 3 characters: m, K and |. Inside a positive character class, the characters are OR-ed, and | is parsed as a literal symbol.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563