简体   繁体   English

.split和Ruby中的正则表达式

[英].split and regular expression in Ruby

I want to split my string by (',') but ignore ',' if they are inside quotes. 我想用(',')分割我的字符串,但是如果它们在引号内,则忽略','。 For example 例如

 " 2,2,4,'hello', 'world', 'hi, there' " 

I want to split it so 'hi, there' will not be split into two different array elements in ruby. 我想将其拆分为“嗨,在ruby中不会拆分为两个不同的数组元素”。 How can I do that? 我怎样才能做到这一点? Probably use some regex? 可能使用一些正则表达式?

EDIT: IF I use this, (from link to possible dublicate) 编辑:如果我使用此,(从链接到可能的重复)

 values = values.split(',(?=([^\\"]*\\"[^\\"]*\\")*[^\\"]*$)', -1) 

my string is split correctly, but now I can not use .delete_at() method on my array. 我的字符串正确分割,但是现在我不能在数组上使用.delete_at()方法。 Before I could do: 在我可以做之前:

 values.delete_at(20) 

Very well. 很好。 Taking inspiration from this answer , the regular expression you are looking for is: 这个答案中得到启发,您正在寻找的正则表达式为:

values.split(/,(?=(?:[^']*'[^']*')*[^']*$)/)

This will not work if you have escaped quotes, for example ( eg "'O\\'Reilly\\'s car'" ). 例如,如果转义了引号,则此方法将无效( eg "'O\\'Reilly\\'s car'" )。

However, this looks a bit like an XY problem. 但是,这看起来有点像XY问题。 If you want to parse CSV, as it seems, and if this was a compliant CSV, you could use CSV.parse or CSV.parse_line . 如果您想解析CSV(看起来好像),并且这是兼容的CSV,则可以使用CSV.parseCSV.parse_line It is not, due to extra spaces between column separators. 并不是因为列分隔符之间有多余的空间。 Using standard formats and standard parsers is, if possible, almost always preferable to home-grown solutions. 如果可能,使用标准格式和标准解析器几乎总是比本地解决方案更可取。

Here's a non-regex solution: 这是一个非正则表达式的解决方案:

str = " 2,2,4,'hello', 'world', 'hi, there' "

first_quote_read = false 
str.each_char.with_object (['']) do |c,a|
  if c == ?, && !first_quote_read
    a << ''
  else
    a[-1] << c
    first_quote_read = !first_quote_read if c == ?'      
  end
end
  #=> [" 2", "2", "4", "'hello'", " 'world'", " 'hi, there' "] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM