# Regexing for 2 periods? \`..\`

**URL:** https://discourse.cmake.org/t/regexing-for-2-periods/4066
**Category:** Usage
**Created:** [September 10, 2021, 7:58pm UTC](https://discourse.cmake.org/t/regexing-for-2-periods/4066 "2021-09-10T19:58:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [September 10, 2021, 7:58pm UTC](https://discourse.cmake.org/t/regexing-for-2-periods/4066/1 "2021-09-10T19:58:23Z")

</div>

I’m trying to detecting when a path contains `..` For example:

```auto
D:/foobar/../

```

However I can’t seem to write the correct regex.

```auto
# regex.cmake
cmake_minimum_required(VERSION 3.21)

if ("D:/foobar" MATCHES "\.\.")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

if ("D:/foobar/../" MATCHES "\.\.")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

```

I run this regex and it somehow matches in both cases.

```auto
> cmake -P .\regex.cmake
-- D:
-- D:

```

Why is this happening?

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [September 10, 2021, 7:59pm UTC](https://discourse.cmake.org/t/regexing-for-2-periods/4066/2 "2021-09-10T19:59:23Z")

</div>

The docs say you can use backslash to escape the `.` character:

 ![image](https://discourse.cmake.org/uploads/default/original/2X/c/c78fdf6d0c25ba88ead571a79c6d83efb350d04b.png)

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [September 10, 2021, 10:31pm UTC](https://discourse.cmake.org/t/regexing-for-2-periods/4066/3 "2021-09-10T22:31:01Z")

</div>

The documentation is a bit misleading. You basically need to escape “twice”. Once for the CMake parser itself, and once for the regex engine.

For instance, if you write

```cmake
if ("D:\\foobar" MATCHES "\\")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

```

then the regex engine actually gets `\`, which is not a valid regex, and you get an error like:

```auto
RegularExpression::compile(): Trailing backslash.
RegularExpression::compile(): Error in compile.
CMake Error at D:/dev/CMExt/regex.cmake:3 (if):
  if given arguments:

    "D:\\foobar" "MATCHES" "\\"

  Regular expression "\" cannot compile

```

In your case, you need to write:

```cmake
if ("D:/foobar/../" MATCHES "\\.\\.")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

```

I hope this helps!

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [September 12, 2021, 10:17am UTC](https://discourse.cmake.org/t/regexing-for-2-periods/4066/4 "2021-09-12T10:17:32Z")

</div>

As an alternative you can use a bracket argument `[[\.\.]]` intead of a quoted argument `"\\.\\."`.  
This makes writing RegExes simplier.

This is mentioned in the last sentence of the Regex Specification. Perhaps the docs should mention the right escaping at the top of the chapter.
