ruby - 使用 Ruby on Rails 将字符串拆分成多个点

标签 ruby string

我的数据库中有一个字符串代表用户的注释。我想拆分这个字符串,这样我就可以将每个注释分成内容、用户和日期。

这是字符串的格式:

"Example Note <i>Josh Test 12:53 PM on 8/14/12</i><br><br> Another example note <i>John Doe 12:00 PM on 9/15/12</i><br><br>  Last Example Note <i>Joe Smoe 1:00 AM on 10/12/12</i><br><br>" 

我需要把它分解成一个数组

["Example Note",  "Josh Test", "12:53 8/14/12", "Another example note", "John Doe", "12:00 PM 9/15/12", "Last Example Note", "Joe Smoe", "1:00 AM 10/12/12"]

我还在试验这个。任何想法都非常欢迎谢谢! :)

最佳答案

您可以使用正则表达式来获得更简单的方法。

s = "Example Note <i>Josh Test 12:53 PM on 8/14/12</i><br><br> Another example note <i>John Doe 12:00 PM on 9/15/12</i><br><br>  Last Example Note <i>Joe Smoe 1:00 AM on 10/12/12</i><br><br>" 
s.split(/\s+<i>|<\/i><br><br>\s?|(?<!on) (?=\d)/)
=> ["Example Note", "Josh Test", "12:53 PM on 8/14/12", "Another example note", "John Doe", "12:00 PM on 9/15/12", " Last Example Note", "Joe Smoe", "1:00 AM on 10/12/12"]

日期时间元素是非格式的,但单独对它们应用一些格式也许是可以接受的。

编辑:删除了不必要的 + 字符。

关于ruby - 使用 Ruby on Rails 将字符串拆分成多个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864346/

相关文章:

ruby-on-rails - Docker:Docker-compose ruby​​ + postgres - PG::ConnectionBadcould not connect to server

html - 在 Rails 中呈现 View 后评估代码

c# - 替代 String.Replace

php - 如何在 PHP 中自然地对文件列表(一些以数字结尾,例如 "-2")进行排序

java - 从 String ArrayList 获取输入并输出新字符串

java - 在 java 字符串池中将创建多少个 String 对象?

ruby-on-rails - 我如何在我的 rspec 测试中考虑随机数?

ruby - 为什么 ruby​​ 代码在我第二次调用 exec 后崩溃/退出?

ruby-on-rails - 错误:执行 gem 时 ...(Gem::Exception)OpenSSL 不可用

python - 查找并剪切出一个python子串