I am trying to match a word which has no special characters attached at both front and back.
The regex I have written is /\btest\b/gi
I have added two word boundaries at front and back for matching the exact word.
The sample text is :
This is a test.
The testing was good.
The tester was bad.
package.test.com
[email protected]
Regex-test-is tough
"rm/[email protected]"
/Users/dattem/Desktop/DevCenter/test/ssl/
The test is today.
Name -test one
I want to match the test word of only first and last two lines. https://regex101.com/r/AEfzUw/4
CodePudding user response:
The following regex will match only the first and last 2 lines in your cases:
(?<=[\s\t-])test(?=[\s\.\t])
https://regex101.com/r/RCfs8l/1
It uses positive lookbehind and lookahead:
(?<=[\s\t-])
a space, tab or dashtest
test(?=[\s\.\t])
followed by a space, a dot or tab
CodePudding user response:
This does the trick:
(?<= )test\b|\btest(?= )
The word test
either preceded or followed by a space. I don't think there is a way to avoid repeating the word test
in the regex.