-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怪異輸出
謝謝
很酷,感謝您的信息。我完全錯過了文檔的解釋。 – Diego
正則表達式文檔有點蔓延,它們不會使它特別容易被發現。樂意效勞。 –