2016-02-12 85 views
-2
irb(main):021:0> TEXT = <<EOF 
irb(main):022:0" Text messaging, or texting, is the act of composing 
and sending electronic messages between two or more mobile phones, or fixed or 
portable devices over a phone network. The term originally referred to messages 
sent using the Short Message Service (SMS). 
It has grown to include multimedia messages(known as MMS) containing images, videos, 
and sound content, as well as ideograms known as emoji 
irb(main):023:0" EOF 
=> "Text messaging, or texting, is the act of composing and sending electronic 
messages between two or more mobile phones, or fixed or portable devices over a 
phone network. The term originally referred to messages sent using the Short 
Message Service (SMS). It has grown to include multimedia messages (known as MMS) 
containing images, videos, and sound content, as well as ideograms known as emoji\n" 

irb(main):064:0> LEXICON = { 
irb(main):065:1* "Text" => "WRITING", 
irb(main):066:1* "is" => "WAS" 
irb(main):067:1> } 
irb(main):070:0> sanitized = TEXT.gsub(pattern, LEXICON) 
=> "WRITING messaging, or texting, WAS the act of composing and sending electronic messages between two or more mobile phones, or fixed or portable devices over a phone network. The term originally referred to messages sent using the Short Message Service (SMS). It has grown to include multimedia messages (known as MMS) containing images, videos, and sound content, as well as ideograms known as emoji\n" 
irb(main):071:0> terms = LEXICON.keys.map {|k| Regexp.new(Regexp.escape(k))}.join("|") 
=> "(?-mix:Text)|(?-mix:is)" <------------ What is this (?-mix:...) thing? 

irb(main):072:0> sanitized = TEXT.gsub(pattern, LEXICON) 
=> "WRITING messaging, or texting, WAS the act of composing and sending electronic 
messages between two or more mobile phones, or fixed or portable devices over a 
phone network. The term originally referred to messages sent using the Short 
Message Service (SMS). It has grown to include multimedia messages (known as MMS) 
containing images, videos, and sound content, 
as well as ideograms known as emoji\n" 

我看Ruby的小吃,情節190,我試了一下在IRB會話,並且我會得到一個很有趣的我嗎?(-mix:鍵)|( - 混合:鍵)表達之後。有人能向我解釋那是什麼嗎?紅寶石Regexp.escape怪異輸出

謝謝

回答

0

這是covered in the docs(?-mix:...)禁用圓括號內子表達式的mix選項。

P.S.一個更簡單的方式做到這一點:

LEXICON.keys.map {|k| Regexp.new(Regexp.escape(k)) }.join("|") 

是這樣的:

Regexp.union(LEXICON.keys) 

假設你最終要的一個正則表達式,而不是一個字符串。請參閱Regexp.union

+0

很酷,感謝您的信息。我完全錯過了文檔的解釋。 – Diego

+0

正則表達式文檔有點蔓延,它們不會使它特別容易被發現。樂意效勞。 –