how do you put special characters such as(^) as regular strings for regex

I need to find ^ but the regex just sees it as the special letter, anyways to just make it a regular string?
Probably depends on which specific Regex implementation you are using!

But, generally, characters that have a "special" meaning in Regex are escaped by prepending a \ character. Though keep in mind that the \ character already acts as an escape character C/C++ string literals!


For example, the following Regex matches any (non-empty) sequence of characters:
.+

...because the . is a "wildcard" character in Regex.


At the same time, this Regex only matches a sequence of literal dot (.) characters:
\.+


And, written as a C/C++ string literal, the above Regex then becomes:
"\\.+"

...because, in a C/C++ string literal, the sequence \\ translates to a single \ character.
Last edited on
you escape it

R"(\^)"
The use of raw string literals is correct, but I do recommend using a delimiter in regular expression strings — leaving it blank will bite you.

I personally prefer using “<>” for my raw literals; the likelihood of that conflicting with anything is low, making it easier for me to parse its utility. Plus it is really easy to view, IMHO.

For example, this arcane bit of RE:

    R"<>((\S+)(\s+\1){4})<>"

I think it is pretty easy to find just the

        ((\S+)(\s+\1){4})

out of that; all you need to do is remember that the outer parentheses are also not part of the RE and you are golden:

         (\S+)(\s+\1){4}

...which is:

  • capture some non-whitespace sequence (the first parenthesized part),

  • then exactly four more of the same sequence with leading whitespace
    (the second parenthesized part and the count=4 requirement in curly braces).

This is, of course, my personal opinion. Take it as you wish.
Topic archived. No new replies allowed.