解析每次可能具有不同值的字符串

时间:2015-03-16 16:54:12

标签: ruby

我有以下字符串:

"---\n- :@error: 'Invalid phone number format: ''''.  Please check that your format  for\n    phone number is correct.'\n- :@error: 'Invalid id was sent: '\n- :@error: 'Invalid date format: '\n"

我试图只返回错误消息。那就是

  • "Invalid phone number format:"
  • "Please check that your format for phone number is correct."
  • "Invalid id was sent:"
  • "Invalid date format:"

将返回的某些消息将与上面的消息不同,因此我不认为使用正则表达式匹配将是最好的方法。有什么想法我只能从这个字符串中提取错误消息吗?

5 个答案:

答案 0 :(得分:2)

试试这个,它并不完美,但要做到这一点。

s = "---\n- :@error: 'Invalid phone number format: ''''.  Please check that your format  for\n    phone number is correct.'\n- :@error: 'Invalid id was sent: '\n- :@error: 'Invalid date format: '\n"
# scan will get the strings between quotes that are not themselves quotes
# it returns an array of arrays
# flatten will make it a single array
# gsub then strip will normalize the results.
msgs = s.scan(/'([^']+)'/).map{ |(msg)| msg.gsub(/(\.|\s+)/, ' ').strip }

#["Invalid phone number format:", 
# "Please check that your format for phone number is correct", 
# "Invalid id was sent:", 
# "Invalid date format:"]

答案 1 :(得分:1)

尝试将字符串拆分为@error

error_messages = string.split('@error')
error_messages.each {|e| p e.gsub(/[\n:'-]/, '').strip}

返回以下内容

"Invalid phone number format .  Please check that your format for    phone number is correct."
"Invalid id was sent"
"Invalid date format"

我已经非常接近,我只需要玩这个,看看我是否可以编辑空白。

答案 2 :(得分:1)

这是一个YAML字符串,所以请对其进行处理。你的生活会更轻松:

require 'yaml'

str = "---\n- :@error: 'Invalid phone number format: ''''.  Please check that your format  for\n    phone number is correct.'\n- :@error: 'Invalid id was sent: '\n- :@error: 'Invalid date format: '\n"

error = YAML.load(str)
error = YAML.load(str)
# => [{:@error=>
#       "Invalid phone number format: ''.  Please check that your format  for phone number is correct."},
#     {:@error=>"Invalid id was sent: "},
#     {:@error=>"Invalid date format: "}]

我们现在可以看到它是一系列错误。看第一个:

error[0] # => {:@error=>"Invalid phone number format: ''.  Please check that your format  for phone number is correct."}

现在可以轻松访问错误:

error[0][:@error] # => "Invalid phone number format: ''.  Please check that your format  for phone number is correct."

答案 3 :(得分:0)

your_string.scan(/@error: '(.*): ?'/)将返回错误消息数组。

答案 4 :(得分:0)

在我看来,每个错误消息(开头的"---\n"除外)都以连字符开头,以换行符结束。如果是这样,您可以按如下方式提取它们:

str = "---\n- :@error: 'Invalid phone number format: ''''.  Please check that your format  for\n    phone number is correct.'\n- :@error: 'Invalid id was sent: '\n- :@error: 'Invalid date format: '\n"

str.scan(/-.*?\n(?=-|$)/m)[1..-1]
  #=> ["- :@error: 'Invalid phone number format: ''''.  Please check that your format  for\n    phone number is correct.'\n",
  #    "- :@error: 'Invalid id was sent: '\n",
  #    "- :@error: 'Invalid date format: '\n"]

注意:

  • [1..-1]会丢弃初始匹配"---\n"
  • 多行修饰符/m是必需的,因为每条错误消息都可能包含换行符。
  • {li} ? .*?使.*懒惰。没有它,包含一个元素的数组(整个字符串)由str.scan(/-.*\n(?=-|$)/m)返回,因此以下[1..-1]返回一个空数组。
  • 正向前瞻(?=-|$)/m)可确保错误消息的结尾紧跟一个连字符或字符串的结尾。