Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to exclude in RegEx GREP search pattern matches

Explorer ,
Jan 28, 2025 Jan 28, 2025

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.
TOPICS
How-to , Scripting
354
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 28, 2025 Jan 28, 2025

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

/^TEST(?=\s|$)
Translate
Adobe
Community Expert ,
Jan 28, 2025 Jan 28, 2025

try this pattern

 

/^TEST(?=\s)/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 28, 2025 Jan 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.

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2025 Jan 28, 2025

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

/^TEST(?=\s|$)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 28, 2025 Jan 28, 2025

That's the one ... short and sweet. Thanks.

 

// original
/(^TEST #(\d{1,2})(\s[\s\S]+)?)$/

// with OR
/^TEST #(\d{1,2})(?=\s|$)/

 

Screenshot 2025-01-28 at 7.22.57 PM.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2025 Jan 28, 2025

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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2025 Jan 29, 2025
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines