Skip to main content
Known Participant
January 28, 2025
Answered

How to exclude in RegEx GREP search pattern matches

  • January 28, 2025
  • 1 reply
  • 1102 views

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.
Correct answer m1b

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

/^TEST(?=\s|$)

1 reply

CarlosCanto
Community Expert
January 28, 2025

try this pattern

 

/^TEST(?=\s)/
nutradialAuthor
Known Participant
January 28, 2025

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.

 

 

 

 

m1b
m1bCorrect answer
Community Expert
January 28, 2025

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

/^TEST(?=\s|$)