Copy link to clipboard
Copied
I am trying to validate my RegEx GREP search match in the following way:
Match results in the string (TEST)
The word TEST starts the pattern (^TEST) but a white space (\s) is required for any other character to follow.
Valid strings:
1 Correct answer
@nutradial by the way, you can combine them using an OR:
/^TEST(?=\s|$)
Explore related tutorials & articles
Copy link to clipboard
Copied
try this pattern
/^TEST(?=\s)/
Copy link to clipboard
Copied
Better together ... thanks for your help and for teaching me the use of the positive lookahead and how it does not include it in the results.
/(^TEST$)?(^TEST(?=\s))?/
This seems to work a little better ... it takes care of the TEST1 or TESTa as that they were being included in the original search pattern.
Copy link to clipboard
Copied
@nutradial by the way, you can combine them using an OR:
/^TEST(?=\s|$)
Copy link to clipboard
Copied
That's the one ... short and sweet. Thanks.
// original
/(^TEST #(\d{1,2})(\s[\s\S]+)?)$/
// with OR
/^TEST #(\d{1,2})(?=\s|$)/
Copy link to clipboard
Copied
m1b's way is working fine. I will add another method. Since \b represents a word boundary, if you want to force a word to have a boundary, you can use this.
/^TEST\b/
Copy link to clipboard
Copied
I am implementing pattern matches in several scripts once @m1b showed me their utility a while back. I've spent time on the RegEx and GREP reference materials and my patterns and group result have only grown in complexity. As they say "less is more" ... the nuances in this post are next-level:
- Matching a group after the main expression without including it in the result by using "lookarounds".
- Using "OR" and its ability to condense the pattern results. In the case of this post using the alternation produces a direct match without creating a group.
- Using word boundaries to match between a word character and non-word character or position.
I'll be revising my patterns and surely condensing them as a result of this post.

