Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for single space

Tags:

regex

I'm trying to match a file which is delimited by multiple spaces. The problem I have is that the first field can contain a single space. How can I match this with a regex?

Eg:

Name           Other Data    Other Data 2 
Bob Smith      XX1           0101010101
John Doe       XX2           0101010101
Bob Doe        XX3           0101010101
John Smith     XX4           0101010101

Can I split these lines into three fields with a regex, splitting by a space but allowing for the single space in the first field?

like image 862
Echilon Avatar asked Nov 22 '12 15:11

Echilon


1 Answers

I think the simplest is to use a regex that matches two or more spaces.

/  +/

Which breaks down as... delimiter (/) followed by a space () followed by another space one or more times (+) followed by the end delimiter (/ in my example, but is language specific).

So simply put, use regex to match space, then one or more spaces as a means to split your string.

like image 156
Billy Moon Avatar answered Sep 28 '22 08:09

Billy Moon