如何在字符串中找到@ [XX:XXXX]的所有实例,然后找到周围的文本?

时间:2011-10-31 21:05:45

标签: ruby-on-rails ruby regex ruby-on-rails-3

给出一个字符串:

"@[19:Sara Mas] what's the latest with the TPS report? @[30:Larry Peters] can you help out here?"

我想找到一种动态返回方式,标记用户和周围内容。结果应该是:

user_id: 19
copy: what's the latest with the TPS report?

user_id: 30
copy: can you help out here?

有关如何使用ruby / rails进行此操作的任何想法?感谢

这个正则表达式如何找到匹配?

@\[\d+:\w+\s\w+\]

2 个答案:

答案 0 :(得分:2)

拆分字符串,然后迭代处理内容。我认为这不会超过:

tmp = string.split('@').map {|str| [str[/\[(\d*).*/,1], str[/\](.*^)/,1]] }
tmp.first #=> ["19", "what's the latest with the TPS report?"]

这有帮助吗?

答案 1 :(得分:1)

result = subject.scan(/\[(\d+).*?\](.*?)(?=@|\Z)/m)

分别在反向引用1和2中获取id和内容。要停止捕获,必须满足@或字符串结尾。

 "
\\[         # Match the character “[” literally
(          # Match the regular expression below and capture its match into backreference number 1
   \\d         # Match a single digit 0..9
      +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
.          # Match any single character that is not a line break character
   *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\\]         # Match the character “]” literally
(          # Match the regular expression below and capture its match into backreference number 2
   .          # Match any single character that is not a line break character
      *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?=        # Assert that the regex below can be matched, starting at this position (positive lookahead)
              # Match either the regular expression below (attempting the next alternative only if this one fails)
      \@          # Match the character “\@” literally
   |          # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \$          # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
"

这将匹配从@开头到标点符号makr的内容。对不起,如果我没有正确理解。

result = subject.scan(/@.*?[.?!]/)