Dariel Pratama
Dariel Pratama

Reputation: 1675

find word that each character separated by space

I need a regex to select a word that each char on that word separated by whitespace. Look at the following string

Mengkapan,Sungai Apit,S I A K,Riau,

I want to select S I A K. I am stuck, I was trying to use the following regex

\s+\w{1}\s+

but it's not working.

Upvotes: 1

Views: 73

Answers (3)

The fourth bird
The fourth bird

Reputation: 163457

You could match a word boundary \b, a word character \w and repeat at least 2 times a space and a word character followed by a word boundary:

\b\w(?: \w){2,}\b

Regex demo

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

I suggest

\b[A-Za-z](?:\s+[A-Za-z])+\b

pattern, where

\b           - word boundary
[A-Za-z]     - letter (exactly one)
(?:          - one or more groups of  
   \s+       - white space (at least one)
   [A-Za-z]  - letter (exactly one)
)+
\b           - word boundary

Upvotes: 2

Jan
Jan

Reputation: 43169

For your given information, you could use

(?:[A-Za-z] ){2,}[A-Za-z]

See a demo on regex101.com.

Upvotes: 2

Related Questions