2011-10-31 25 views
1

給予相同的字符串:如何在字符串中查找@ [XX:XXXX]的所有實例,然後查找周圍的文本?

"@[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+\] 
+0

http://rubular.com/ – Ryanmt

+0

rubular可能會幫助w @ [XX:XXXXX]但沒有循環或找到周圍的內容嗎? –

+0

我有這樣的正則表達式,@ \ [\ d +:\ w + \ s \ w + \]但現在我該去哪裏? –

回答

1
result = subject.scan(/\[(\d+).*?\](.*?)([email protected]|\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) 
) 
" 

這將匹配從@開始並結束於標點符號的內容。對不起,如果我不正確理解。

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

謝謝,剛剛嘗試過。幾個問題。數字19或30是user.id,所以它可以從1到無限長整數。另外,如果字符串是「與報告@ [30:Larry Peters]有什麼關係」,則上述內容返回「」。有沒有一種方法可以讓內容在完成比賽的整個句子? –

+0

只能處理2位數字的用戶ID。 – Ryanmt

+0

@Ryanmt我沒有看到一個數字較大的例子。這個微不足道的事情可以簡單地用\ d +來修正。感謝您的提示雖然.. – FailedDev

2

拆分字符串,然後迭代處理內容。我認爲這不會超過:

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

這有幫助嗎?

+0

有趣,謝謝你爲什麼拆分字符串?找到正則表達式匹配的所有位置會很好。然後抓住周圍的內容,直到標點符號的第一個符號(句號,問號,感嘆號)。想法? –

+1

當然,這是可能的。我認爲調試起來比較簡單,而且稍後可能不太容易閱讀,因爲當你忘記REGEX應該做什麼的時候。 – Ryanmt

+0

不知道這是什麼意思,請參閱其他問題的答覆... –

相關問題