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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With