-4

Sentence = I will buy a red car, a black racecar or a blue caravan

Pattern = car

Result: should match car and caravan (partial or whole word anywhere in the sentence). Should not match racecar (not the beginning of a word)

Edit: This question is not a duplicate of Regex match entire words only because that question specifically asks NOT to match partial words. My requirement is to match partial OR whole words.

PrashanD
  • 2,643
  • 4
  • 28
  • 58
  • You should always look for some [quick reference guide](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference) if you are not an expert (like me). – Leonardo Alves Machado Dec 24 '18 at 10:19
  • 1
    @LeonardoAlvesMachado: This is a reference for .NET only. A better reference is https://www.regular-expressions.info/ – Toto Dec 24 '18 at 10:23
  • @CertainPerformance This question is not a duplicate of Regex match entire words only because that question specifically asks NOT to match partial words. My requirement is to match partial OR whole words. – PrashanD Dec 24 '18 at 11:08
  • 1
    The principle is exactly the same. To check identify the boundary between a word character and a non-word character, use a word boundary. – CertainPerformance Dec 24 '18 at 11:17

1 Answers1

0

Just add a wordboundary at the beginning:

\bcar\w*

DEMO

Toto
  • 89,455
  • 62
  • 89
  • 125