-5

How can I match a space without a leading comma?

My use case is I would like to split a string on space without a leading comma e.g. "id 10, 11"

Elvyn Mejia
  • 307
  • 4
  • 18
  • without a leading comma? you could `string.split(%r{\s})` – Josh Brody Nov 15 '17 at 22:05
  • Yup so basically for a string `"id 10, 11"` I only care about "id 10". After splitting I would end up with `["id", "10"]` – Elvyn Mejia Nov 15 '17 at 22:11
  • Josh Brody, it splits on every space: ["id", "10,", "11"] – eugene-nikolaev Nov 15 '17 at 22:17
  • 2
    What if there are two spaces after `,`? Should it split on the second space? Also, see https://ideone.com/XTcSsP. Your question is too unclear. Please explain the expected output for the given example string, and why it should look like that. – Wiktor Stribiżew Nov 15 '17 at 22:24
  • 1
    Without an idea of what you're looking to get as output, this is very confusingly worded. Also you haven't attempted to solve the problem, so I'm voting to close. – Mark Thomas Nov 15 '17 at 23:34

3 Answers3

2

You need a lookbehind regex:

 'id 10, 11'.split(/(?<=[^,])[ ]/)

Output:

=> ["id", "10, 11"]
eugene-nikolaev
  • 1,290
  • 1
  • 13
  • 21
1

No need for a regex, just split twice:

"id 10, 11".split(",").first.split #=> ["id", "10"]
steenslag
  • 79,051
  • 16
  • 138
  • 171
0

You could use the regular expression

r = /(?<!,) /

which reads, "match one space not preceded by a space" ((?!,) being a negative lookbehind).

"fe, fi, fo and fum".split r
   #=> ["fe, fi, fo", "and", "fum"]
"fe, fi,     fo\nand fum".split r
   #=> ["fe, fi, ", "", "", "", "fo\nand", "fum"]

If one wishes to break on one or more spaces not preceded by a comma, use

r = /(?<!,) +/
"fe, fi,     fo\nand fum".split r
  #=> ["fe, fi, ", "fo\nand", "fum"]

To split on one or more whitespace characters not preceded by a comma, use

r = /(?<!,)\s+/
"fe, fi,     fo\nand fum".split r
  #=> ["fe, fi, ", "fo", "and", "fum"]

It may prudent to first perform String#lstrip.

r = /(?<!,)\s+/
" fe, fi,     fo\nand fum ".split r
  #=> ["", "fe, fi, ", "fo", "and", "fum"]
" fe, fi,     fo\nand fum ".lstrip.split r
  #=> ["fe, fi, ", "fo", "and", "fum"]

Depending on requirements, one could instead take @elyvn's advice and write

r = /(?<=[^,])\s+/

which reads, "match one or more whitespace characters preceded by a character other than a comma" ((?<=[^,]) being positive lookbehind).

" fe, fi,     fo\nand fum ".split r
  #=> [" fe, fi, ", "fo", "and", "fum"]
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100