Aisen Wang
Aisen Wang

Reputation: 67

How to write a regular expression to find the line starts with at least one word

How to find the line that starts with at least one dog from the following lines

-catdogcat [pass]
-dog------ [pass]
-catdoglol dog [pass]
-catcatcat dog [fail]

I wrote /^-(dog){1,3}/ but it certainly doesn't work when the input is '-catdogcat'

Upvotes: 0

Views: 39

Answers (1)

ndnenkov
ndnenkov

Reputation: 36101

/^-\S*dog/

The idea is to start with a dash, followed by any amount of non-whitespace characters, followed by a dog.


If you want to ensure that each word should be 3 characters long, you could be more explicit:

/^-(\S{3})*dog/


And if you want each word to be 3 characters long and everything to start with three words, you can be even more explicit:

/^-(\S{3}){0,2}dog/

Upvotes: 1

Related Questions