2009-08-01 39 views
7

我想找到一種方法讓我從一個字符串(從數據庫中取出)動態創建一個正則表達式對象,然後用它來過濾另一個字符串。這個例子是從git提交消息中提取數據,但理論上任何有效的正則表達式都可以作爲字符串出現在數據庫中。Ruby中的動態正則表達式

會發生什麼

>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" 
>> r = Regexp.new("[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+") 
>> string[r] 
=> nil 

我希望發生

>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" 
>> string[/[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/] 
=> "Project: Revision 123456 committed by Me" 

回答

11

你唯一缺少的一件事是什麼:

>> Regexp.new "\w" 
=> /w/ 
>> Regexp.new "\\w" 
=> /\w/ 

反斜槓在字符串轉義字符。如果你想要一個文字反斜槓,你必須加倍。

>> string = "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" 
=> "[ALERT] Project: Revision ...123456 committed by Me <[email protected]>\n on 2009- 07-28 21:21:47\n\n Fixed typo\n" 
>> r = Regexp.new("[A-Za-z]+: Revision ...[\\w]+ committed by [A-Za-z\\s]+") 
=> /[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/ 
>> string[r] 
=> "Project: Revision ...123456 committed by Me " 

通常情況下,如果你願意粘貼從「破」行,而不僅僅是輸入輸出,你可能看準了ws不正確轉義

+0

完美,感謝 - 我知道我必須做一些微妙的錯誤。 – davidsmalley 2009-08-01 08:13:57

0

選項1:

# Escape the slashes: 
r = Regexp.new("[A-Za-z]+: Revision ...[\\w]+ committed by [A-Za-z\\s]+") 

缺點:手動逸出所有已知的轉義字符

選項2:

# Use slashes in constructor 
r = Regexp.new(/[A-Za-z]+: Revision ...[\w]+ committed by [A-Za-z\s]+/) 

缺點:無

+0

對於選項2 - 構造函數的參數總是字符串,因爲正在從數據庫中提取正則表達式,所以在這種情況下不起作用。 – davidsmalley 2009-08-01 08:16:58