user825900
user825900

Reputation:

Find words with specified first letter (Regex)

I need regex to find words starting, for example, whith letters "B" or "b". In sentence Bword abword bword I need to find Bword and bword. My curresnt regex is: [Bb]\w+ (first character is space), but it doesn't find Bword.

Thanks in advance.

Upvotes: 2

Views: 807

Answers (3)

Ameya Pandilwar
Ameya Pandilwar

Reputation: 2778

The pattern for that should be - "[Bb]\w+"

You need to escape the backslashes (with another backslash) in a regular expression. \b --> \b

Upvotes: 0

Howard
Howard

Reputation: 39197

You can use the word boundary pattern \b to match boundaries between words or start/end:

\b[Bb]\w*\b

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Try using following regex: (?i)\bB\w*\b

It means:

  1. (?i) - turn on ignore case option
  2. \b - first or last character in a word
  3. B
  4. \w* - Alphanumeric, any number of repetitions
  5. \b - first or last character in a word

So it will find Bword and bword.

Upvotes: 1

Related Questions