'How to match a word with no special character at front and back
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
Solution 1:[1]
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 dashtesttest(?=[\s\.\t])followed by a space, a dot or tab
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Diego De Vita |
| Solution 2 | Thomas |
