开发者

Find words with specified first letter (Regex)

开发者 https://www.devze.com 2023-03-17 01:19 出处:网络
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.

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.


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.


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

\b[Bb]\w*\b


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消