Copy link to clipboard
Copied
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:
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!
Copy link to clipboard
Copied
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\[]]
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now