Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Strange behavior with brackets inside character class

Explorer ,
Jul 05, 2025 Jul 05, 2025

I've encountered some unexpected behavior with brackets inside character classes in InDesign GREP and would like to get your expert opinions.

 

Test text: ab a[c] [d]

 

Test results:

  • [a-z[] → matches: a, b, a, [, c, [, d (as expected)
  • [a-z]] → matches: c], d] (two characters together)
  • [a-z[]] → matches: c], d] (same as above)
  • [a-z\[\]] → matches: a, b, a, [, c, ], [, d, ] (as expected)

 

Question: Why do [a-z]] and [a-z[]] produce the same result, matching c] and d] as two-character sequences rather than individual characters?

 

Logically, [a-z[]] should match "lowercase letters OR opening bracket OR closing bracket" individually, but it seems to behave differently.

 

Is this a bug, or am I missing something about how InDesign GREP handles nested brackets in character classes?

 

Workaround: Using [a-z\[\]] (escaping both brackets) works as expected.

 

Thanks for any insights!

TOPICS
Performance
81
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2025 Jul 05, 2025

I have to say, I don't know why.

 

I simply accepted it as cargo-cult StackExchange gospel that these glyphs

.^$*+?()[{\|

must be escaped. I am not able to say exactly how InDesign's regex differs from typical PCRE regex. (something something Boost libraries?)  But, if open bracket must be escaped, then the following is not the case:

 

Logically, [a-z[]] should match "lowercase letters OR opening bracket OR closing bracket" individually, but it seems to behave differently.

 

To match what you think it'd match logically, I assume you'd need to escape the open bracket:

[a-z\[]]

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 06, 2025 Jul 06, 2025
LATEST

I wouldn't have expected [a-z[] to match anything at all. Like Joel, I always escape brackets inside character classes. For me, "Using [a-z\[\]] (escaping both brackets)" is not a workaround but the preferred, if not required, form.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines