简体   繁体   中英

Regex to match all groups between more than one space

I've got a string

198.21 543 G110P0GHTT SAW GHA + DBA 11998

And I'd like to match all groups of the string between spaces. So far I've come up with (?<=\\s)(.*?)(?=\\s) which matches all but the first group. Additionally, this does not count the GHA + DBA as a group. What can I add to this to ensure it includes the first record as well as anything MORE than one space

You don't need to use look arounds here. Just use this regex to match a non-whitespace string or substring separated by a single space:

\S+(?:\s\S+)*

RegEx Demo

RegEx Details:

  • \\S+ : Match 1+ non-space characters
  • (?:\\s\\S+)* : Match 0 or more non-space substring separated by a single space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM