4

By default, the #split method work as follows:

"id,name,title(first_name,last_name)".split(",")

will give you following output:

["id", "name", "title(first_name", "last_name)"]

But I want something like following:

["id", "name", "title(first_name,last_name)"]

So, I use following regex (from the this answer) using split to get desired output:

"id,name,title(first_name,last_name)".split(/,(?![^(]*\))/)

But, again when I use another string, which is my actual input above, the logic fails. My actual string is:

"id,name,title(first_name,last_name,address(street,pincode(id,code)))"

and it is giving following output:

["id", "name", "title(first_name", "last_name", "address(street", "pincode(id,code)))"]

rather than

["id", "name", "title(first_name,last_name,address(street,pincode(id,code)))"]
8
  • @nemesv Thanks. Is there any way I can achieve the same in ruby? Any idea? Commented Jan 1, 2018 at 13:15
  • 1
    Spring#Split support regex so you just need to take the pattern from the linked question: "id,name,t­itle(first­_name,last­_name)".sp­lit(/,(?![­^(]*\))/) Commented Jan 1, 2018 at 13:31
  • 1
    @fidato Don't use a regex. You're trying to parse a nested data structure, not split a string by a delimiter. Regex is the wrong tool for this job. Commented Jan 1, 2018 at 16:58
  • 1
    Possible duplicate of Regex to match only commas not in parentheses? Commented Jan 1, 2018 at 18:04
  • 1
    @Toto We've established this is not a duplicate of that question. It has already been closed as a duplicate of it and reopened, and it contains the solution provided there and specifically shows the case for which it doesn't work. Commented Jan 1, 2018 at 18:15

3 Answers 3

3

Updated Answer

Since the earlier answer didn't take care of all the cases as rightly pointed out in the comments, I'm updating the answer with another solution.

This approach separates the valid commas using a separator | and, later uses it to split the string using String#split.

class TokenArrayParser
  SPLIT_CHAR = '|'.freeze

  def initialize(str)
    @str = str
  end

  def parse
    separate_on_valid_comma.split(SPLIT_CHAR)
  end

  private

  def separate_on_valid_comma
    dup = @str.dup
    paren_count = 0
    dup.length.times do |idx|
      case dup[idx]
      when '(' then  paren_count += 1
      when ')' then paren_count -= 1
      when ',' then dup[idx] = SPLIT_CHAR if paren_count.zero?
      end
    end

    dup
  end
end

%w(
  id,name,title(first_name,last_name)
  id,name,title(first_name,last_name,address(street,pincode(id,code)))
  first_name,last_name,address(street,pincode(id,code)),city(name)
  a,b(c(d),e,f)
  id,name,title(first_name,last_name),pub(name,address)
).each {|str| puts TokenArrayParser.new(str).parse.inspect }

# =>
# ["id", "name", "title(first_name,last_name)"]
# ["id", "name", "title(first_name,last_name,address(street,pincode(id,code)))"]
# ["first_name", "last_name", "address(street,pincode(id,code))", "city(name)"]
# ["a", "b(c(d),e,f)"]
# ["id", "name", "title(first_name,last_name)", "pub(name,address)"]

I'm sure this can be optimized more.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer, but it doesn't giving me my desired result either. I want following o/p: ["id", "name", "title(first_name,last_name,address(street,pincode(id,code)))"]
Your code gives me array of 5 strings whereas I need only 3 strings that is id, name and title(first_name,last_name,address(street,pincode(id,code))‌​
@fidato, sorry about that. Edited. Could you check now?
This is a really ill-advised implementation depending on a feature of Ruby that is best avoided. It requires every closing parenthesis to be encountered in the same token.
Updated my answer with another approach that I believe takes care of all these cases mentioned in the problem and comments.
|
3
def doit(str)
  split_here = 0.chr
  stack = 0
  s = str.gsub(/./) do |c|
    ret = c
    case c
    when '('
      stack += 1
    when ','
      ret = split_here, if stack.zero?
    when ')'
      raise(RuntimeError, "parens are unbalanced") if stack.zero?
      stack -= 1
    end
    ret
  end
  raise(RuntimeError, "parens are unbalanced, stack at end=#{stack}") if stack > 0
  s.split(split_here)
end

doit "id,name,title(first_name,last_name)"
  #=> ["id", "name", "title(first_name,last_name)"]
doit "id,name,title(first_name,last_name,address(street,pincode(id,code)))"
  #=> ["id", "name", "title(first_name,last_name,address(street,pincode(id,code)))"]
doit "a,b(c(d),e,f)"
  #=> ["a", "b(c(d),e,f)"]
doit "id,name,title(first_name,last_name),pub(name,address)"
  #=> ["id", "name", "title(first_name,last_name)", "pub(name,address​)"]
doit "a,b(c)d),e,f)"
  #=> RuntimeError: parens are unbalanced
doit "a,b(c(d),e),f("
  #=> RuntimeError: parens are unbalanced, stack at end=["("]

A comma is to be split upon if and only if stack is zero when it is encountered. If it is to be split upon it is changed to a character (split_here) that is not in the string. (I used 0.chr). The string is then split on split_here.

2 Comments

My answer originally had stack defined as an array. I pushed '{' onto stack when '('` was encountered and popped one '{' when '}' was encountered. After reviewing @hallucinations' revised answer I realized it was only the size of stack I was using, so I simplified my answer to make stack a local variable.
I learned something else from @hallucination's revised answer which caused me to make a further edit. I had replaced commas that are not to be split upon with a special character, split on the remaining commas and then replaced the special characters with commas. It makes more sense to replace the commas to be split upon with the special character, which is what hallucations has done, avoiding the need for my last step.
-1

This could be one approach:

"id,name,title(first_name,last_name)".split(",")[0..1] << "id,name,title(first_name,last_name)".split(",")[-2..-1].join

Creating a duplicate string and splitting them both, then combining the first two elements of the first string with the joined last two elements of the second string copy. At least in this specific scenario it would give you the desired result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.