OR Operator with Anchors Behaves Incorrectly
InDesign GREP Bug Report: OR Operator with Anchors Behaves Incorrectly
Summary
InDesign's GREP engine does not handle the OR operator (|) correctly when used with anchors (^ and $). This behavior differs significantly from standard regex engines and produces unexpected, inconsistent results.
Problem Description
When using the OR operator with anchors in InDesign GREP, the anchors are interpreted with incorrect precedence, causing the pattern to behave differently than expected. This issue has been confirmed as a bug by Peter Kahrel, author of "GREP in InDesign."
Test Cases and Results
Test Case 1: ^ab|c
Text: ab ac bc Expected (standard regex): Matches "ab" at line start AND "c" anywhere Actual InDesign result: Only matches "ab" at line start Issue: InDesign interprets as ^(ab|c) instead of (^ab)|c
Test Case 2: ^a|bc
Text: ab ac bc Expected (standard regex): Matches "a" at line start AND "bc" anywhere Actual InDesign result: Only matches "a" at line start Issue: Same precedence problem as Test Case 1
Test Case 3: (^a)|bc
Text: ab ac bc Expected (standard regex): Matches "a" at line start AND "bc" anywhere Actual InDesign result: Still only matches "a" at line start Issue: Even explicit grouping doesn't resolve the problem
Test Case 4: (a$)|bc
Text: ab ac bca Expected (standard regex): Matches "a" at line end AND "bc" anywhere Actual InDesign result: Only matches "bc" ($ anchor completely ignored) Issue: End-of-line anchor is ignored entirely
Test Case 5: bc|(a$)
Text: ab ac bca Expected (standard regex): Matches "bc" anywhere AND "a" at line end Actual InDesign result: Only matches "bc" ($ anchor ignored) Issue: End-of-line anchor ignored regardless of position
Test Case 6: bc|a$
Text: ab ac bca ac ab bc a Expected (standard regex): Matches "bc" anywhere AND "a" at line end Actual InDesign result: No matches at all Issue: Pattern completely fails when $ anchor is second in OR expression
Impact
* Users cannot rely on standard regex behavior for OR operations with anchors
* Complex workarounds are required for simple pattern matching
* Documentation doesn't warn users about this non-standard behavior
* Patterns that work in other regex engines fail unexpectedly in InDesign
Technical Details
* InDesign Version: 20.3.1 x64
Requested Fix
Please fix the OR operator precedence to match standard regex behavior:
1. ^ab|c should match "ab" at line start OR "c" anywhere
2. (a$)|bc should match "a" at line end OR "bc" anywhere
3. Explicit grouping should work as expected
4. End-of-line anchors should not be ignored in OR expressions