Regex split on white space except ones with a colon

时间:2016-10-20 19:19:35

标签: regex

I have the following string

s = "hiack: 18 seqno: 37 cwnd: 20.000 ssthresh: 200 dupacks: 0"

I would like to use a regex to split that line up so it becomes

s = ['hiack: 18' 'seqno: 37' 'cwnd: 20.000' 'ssthresh: 200' 'dupacks: 0']

What regex pattern should I use to achive this?

Edit: I am using python incase that makes a difference

1 个答案:

答案 0 :(得分:3)

% nodejs
> s = "hiack: 18   seqno: 37   cwnd: 20.000 ssthresh: 200 dupacks: 0"
'hiack: 18   seqno: 37   cwnd: 20.000 ssthresh: 200 dupacks: 0'
> s.split(/\b\s+\b/)
[ 'hiack: 18',
  'seqno: 37',
  'cwnd: 20.000',
  'ssthresh: 200',
  'dupacks: 0' ]
> 

Even if you use anything else than JS, you can pick up the regex.

It works using \b aka word boundaries