# Regex match exclude '\]' character

**URL:** <https://discourse.cmake.org/t/regex-match-exclude-character/3348>\
**Category:** Code\
**Created:** [May 17, 2021, 2:59pm UTC](https://discourse.cmake.org/t/regex-match-exclude-character/3348 "2021-05-17T14:59:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Person-93](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@Person-93](https://discourse.cmake.org/u/Person-93)\
**Post date:** [May 17, 2021, 2:59pm UTC](https://discourse.cmake.org/t/regex-match-exclude-character/3348/1 "2021-05-17T14:59:06Z")

</div>

I am trying to use regex to match bracketed text. It seems that cmake doesn’t like when I try to exclude a ‘]’ in middle.  
I tested my regex [here](https://regex101.com/r/RzHF72/1) and it works as expected. I thought it would work for cmake as well. As far as I can tell, I am not using any features that cmake does not support.

```
function(print_var var)
    message(STATUS "${var} = ${${var}}")
endfunction()

set(text "[bracketed text]" )

# this is the pattern I really want
set(pattern "^\\[(([^ \\[\\]\t\r\n]+)( *([^ \\[\\]\t\r\n]+))*)\\]$")
string(REGEX MATCH "${pattern}" output "${text}")
print_var(pattern)
print_var(text)
print_var(output)
print_var(CMAKE_MATCH_1)

# this works but allows ']' in middle of the text
set(pattern "^\\[(([^ \\[\t\r\n]+)( *([^ \\[\t\r\n]+))*)\\]$")
string(REGEX MATCH "${pattern}" output "${text}")
print_var(pattern)
print_var(text)
print_var(output)
print_var(CMAKE_MATCH_1)

# https://regex101.com/r/RzHF72/1
```
