# Regex quoting?

**URL:** https://discourse.cmake.org/t/regex-quoting/10730
**Category:** Usage
**Created:** [April 23, 2024, 10:10pm UTC](https://discourse.cmake.org/t/regex-quoting/10730 "2024-04-23T22:10:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dabrahams](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dabrahams/32/4265_2.png) [@dabrahams](https://discourse.cmake.org/u/dabrahams)
#### Post date: [April 23, 2024, 10:10pm UTC](https://discourse.cmake.org/t/regex-quoting/10730/1 "2024-04-23T22:10:47Z")

</div>

I have a string in a variable that I want to match literally as a regular expression. I want any special regex characters to be interpreted literally. In most environments there’s a “regex quote” operation that transforms a literal string into a corresponding regex. Do we have that for CMake?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 24, 2024, 9:35pm UTC](https://discourse.cmake.org/t/regex-quoting/10730/2 "2024-04-24T21:35:32Z")

</div>

CMake supports lua-style bracket syntax as an alternative to quoting with `"` characters: [https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#bracket-argument](https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#bracket-argument)

---

<div class="post-metadata">

### Author: ![dabrahams](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dabrahams/32/4265_2.png) [@dabrahams](https://discourse.cmake.org/u/dabrahams)
#### Post date: [April 24, 2024, 11:22pm UTC](https://discourse.cmake.org/t/regex-quoting/10730/3 "2024-04-24T23:22:55Z")

</div>

I know about that feature, but I don’t see how it helps. If I understand that feature correctly, it’s a syntax for string literals. But as I wrote above, I don’t have a literal, I have **a string in a variable** that I want to match literally using one of the functions that takes a regex argument. if the string in that variable happens to contain a `*`, for example, I want it to match a `*` rather than being treated as a quantifier. I am looking for a function that knows about the special regex characters and escapes them so they will be interpreted literally, e.g. it replaces `*` with `[*]` or `\*` in the string.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 25, 2024, 11:17am UTC](https://discourse.cmake.org/t/regex-quoting/10730/4 "2024-04-25T11:17:05Z")

</div>

To my knowledge, CMake does not provide anything to do what you’re asking.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 26, 2024, 9:43am UTC](https://discourse.cmake.org/t/regex-quoting/10730/5 "2024-04-26T09:43:28Z")

</div>

[Quoting metacharacters]([perlre - Perl regular expressions - Perldoc Browser](https://perldoc.perl.org/perlre#Quoting) metacharacters)

See a common idiom to disable or quote the special meanings of regular expression metacharacters in a string that you want to use for a pattern. Simply quote all non-“word” characters.
