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(/@.*?[.?!]/)
http://rubular.com/ – Ryanmt
rubular可能會幫助w @ [XX:XXXXX]但沒有循環或找到周圍的內容嗎? –
我有這樣的正則表達式,@ \ [\ d +:\ w + \ s \ w + \]但現在我該去哪裏? –