ro ra
ro ra

Reputation: 419

Match words only starting with a certain letter and then having 5 or more non-whitespace characters

I have tried following regex.

What is correct regex to achieve it?

First, string must start with one of these char specified and then must follow minimum 5 char (no whitespace) char and then it can have anything behind, i.e txxxxx.domain.local, dxxxxx.domain.local but at the same time it should not match with xtxxxx.domain.local or xdxxxx.domain.local.

Upvotes: 1

Views: 110

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627292

In general, if you need to match any word that consists of letters, digits or underscors starting with a specific letter you may use

(?i)\b[tdi]\w*

It will match t, T, d, D, I or i at a word boundary (\b) and then any 0 or more letters, digits or underscores.

You may use

(?i)(?<!\S)[tdi]\S{5,}

See the regex demo

  • (?i) - case insensitive modifier on
  • (?<!\S) - whitespace should come right before the match
  • [tdi] - one of the three letters that the word should start with
  • \S{5,} - five or more non-whitespace chars.

Upvotes: 1

Related Questions