THE BOOK OF RUBY
290
greedy1.rb puts( /.*at/.match('The cat sat on the mat!') ) #=> returns: The cat sat on the mat puts( /.*?at/.match('The cat sat on the mat!') ) #=> returns: The cat
You can control the greediness of pattern matching to do things such as process directory paths: greedy2.rb puts( /.+/.match('C:mydirectorymyfoldermyfile.txt') ) #=> C:mydirectorymyfolder puts( /.+?/.match('C:mydirectorymyfoldermyfile.txt') ) #=> C:
STRING METHODS
Up to now, we’ve used methods of the Regexp class when processing strings. In fact, pattern matching can go both ways due to the fact that the String class has a few regular expression methods of its own. These include =~ and match (so you can switch the order of the String and Regexp objects when matching), plus the scan method which iterates through a string looking for as many matches as possible. Each match is added to an array. Here, for example, I am looking for matches on the letters ‘a’, ‘b’ or ‘c’. The match method returns the first match (‘a’) wrapped up in a MatchData object; but the scan method keeps scanning along the string and returns all the matches it finds as elements in an array: match_scan.rb TESTSTR = "abc is not cba" puts( "
--match--" ) b = /[abc]/.match( TESTSTR ) #=> MatchData: "a" puts( "--scan--" ) a = TESTSTR.scan(/[abc]/) #=> Array: ["a", "b", "c", "c", "b", "a"]
หนังสือของทับทิม290ทำให้ greedy1.rb (/.*at/.match('The แมวนั่งบนเสื่อ!')) #= > กลับ: ให้แมวนั่งบนเสื่อ (/.*?at/.match('The แมวนั่งบนเสื่อ!')) #= > กลับ: แมวคุณสามารถควบคุม greediness ของการจับคู่รูปแบบการต่าง ๆ เช่นกระบวนการเส้นทางไดเรกทอรี: greedy2.rb ใส่ (/.+/.match('C:mydirectorymyfoldermyfile.txt')) #= > ทำให้ C:mydirectorymyfolder (/.+?/.match('C:mydirectorymyfoldermyfile.txt')) #= > c:วิธีการสายอักขระถึงตอนนี้ เราได้ใช้วิธีของคลา Regexp ประมวลผลสตริงการ ในความเป็นจริง จับคู่รูปแบบสามารถไปที่วิธีการทั้งสองเนื่องจากการที่สายอักขระคลาสที่มีวิธีการบางอย่างของนิพจน์ทั่วไปของตัวเอง รวม = ~ และตรงกัน (ดังนั้นคุณสามารถเปลี่ยนลำดับของอักขระและ Regexp วัตถุเมื่อจับคู่), รวมทั้งวิธีการการสแกนซึ่งการคำนวณซ้ำผ่านสตริงที่ค้นหาตรงกันมากที่สุด แข่งขันแต่ละถูกเพิ่มไปยังอาร์เรย์ ที่นี่ เช่น หาตรงตามตัวอักษร 'a', 'b' หรือ 'c' วิธีการจับคู่กลับตรงแรก ('a') ตัดขึ้นในวัตถุ MatchData แต่วิธีการสแกนช่วยให้การสแกนตามสายอักขระ และตรงกันทั้งหมดที่พบเป็นองค์ประกอบในอาร์เรย์: match_scan.rb TESTSTR =ทำให้ "abc ไม่ใช้ข้อตกลง" ("-ตรง- ") b = /[abc]/.match (TESTSTR) #= > MatchData: "a" ("-- "สแกน) การทำให้เป็น = TESTSTR.scan(/[abc]/) #= > เรย์: ["a", "b", "c", "c", "b", "a"]
การแปล กรุณารอสักครู่..

หนังสือของทับทิม290 .greedy1.rb ใส่ ( / * ที่ ตรง ( " แมวนั่งบนเสื่อ ! ' ) # = > ผลตอบแทน : แมวนั่งอยู่บนเสื่อใส่ ( / * ? ที่ ตรง ( " แมวนั่งบนเสื่อ ! ' ) # = > ผลตอบแทน : แมวคุณสามารถควบคุมความโลภของรูปแบบการจับคู่ที่จะทำสิ่งต่าง ๆเช่นเส้นทางไดเรกทอรีกระบวนการ : greedy2.rb ใส่ ( + / / . . ราคา ( ' c : mydirectorymyfoldermyfile . txt ' ) ) # = > C : mydirectorymyfolder ใส่ ( / . + / ตรง ( ' c : mydirectorymyfoldermyfile . txt ' ) ) # = > C :วิธีการสตริงถึงตอนนี้เราได้ใช้วิธีการของ regexp คลาสเมื่อการประมวลผลสตริง ในความเป็นจริงรูปแบบการจับคู่สามารถไปทั้งสองวิธี เนื่องจากสายชั้นมีกี่นิพจน์ปกติวิธีการของตัวเอง เหล่านี้รวมถึง = ~ และราคาเพื่อให้คุณสามารถเปลี่ยนลำดับของสตริงและวัตถุที่ตรงกับ regexp ) บวกกับวิธีการสแกนที่กล่าวย้ำผ่านสตริงมองหาที่ตรงกันมากที่สุด การแข่งขันแต่ละครั้งจะเพิ่มไปยังอาร์เรย์ ที่นี่ ตัวอย่างเช่น ฉันมองหาที่ตรงกับตัวอักษร ' ' , ' ' b ' ' C ' ตรงวิธีการกลับมานัดแรก ( ' ' ) ห่อขึ้นใน matchdata วัตถุ แต่วิธีการสแกนจะสแกนตามแนวสตริงและผลตอบแทนทั้งหมดที่ตรงกับที่พบเป็นองค์ประกอบในอาร์เรย์ : match_scan.rb teststr = " ABC ไม่ใช่ CBA " ใส่ "-- ราคา -- " ) b = / [ ABC ] / ราคา ( teststr ) # = > matchdata " " ใส่ " -- สแกน -- " ) = teststr สแกน ( / [ ABC ] / ) # = > เรย์ : [ " " , " B " , " C " , " C " , " B " , " A " ]
การแปล กรุณารอสักครู่..
