Skip to main content
Known Participant
Jan 28, 2025 2:46 PM UTC
Answered

How to exclude in RegEx GREP search pattern matches

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:

TEST
TEST 123
TEST abc
 
Invalid strings:
TEST1
TESTa
 
Looking for a method where the pattern match returns the string (TEST) and not (TEST ) with the white space included. At the same time the match needs to fail if any character other than the white space immediatly follows TEST.
m1b
m1bCorrect answer
Community Expert
Jan 28, 2025 9:50 PM UTC

@nutradial by the way, you can combine them using an OR:

/^TEST(?=\s|$)

6 replies

CarlosCanto
Community Expert
Community Expert
Jan 28, 2025 4:44 PM UTC

try this pattern

 

/^TEST(?=\s)/
Translate
nutradialAuthor
Known Participant
Jan 28, 2025 5:40 PM UTC

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.

 

 

 

 

Translate
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Jan 28, 2025 9:50 PM UTC

@nutradial by the way, you can combine them using an OR:

/^TEST(?=\s|$)
Translate