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 ของรูปแบบการจับคู่การต่าง ๆ เช่นกระบวนการเส้นทางไดเรกทอรี: (/.+\/.match('C:mydirectorymyfoldermyfile.txt')) ทำให้ greedy2.rb #= > (/.+?\/.match('C:mydirectorymyfoldermyfile.txt')) ทำให้ C:mydirectorymyfolder #= > C:อักขระวิธีถึงตอนนี้ เราได้ใช้วิธีของคลา Regexp สายประมวลผล ในความเป็นจริง รูปแบบการจับคู่สามารถไปวิธีทั้งสองเนื่องจากข้อเท็จจริงที่ว่าระดับสายได้กี่วิธีปกตินิพจน์ของตัวเอง รวม = ~ และจับคู่ (เพื่อให้คุณสามารถสลับลำดับของอักขระและ Regexp วัตถุเมื่อจับคู่), และวิธีการสแกนซึ่งคำนวณซ้ำผ่านสตริงที่ค้นหาตรงกันมากที่สุด ตรงแต่ละจะถูกเพิ่มไปยังอาร์เรย์ ที่นี่ เช่น ฉันกำลังมองหาตรงบนตัวอักษร 'a', 'b' หรือ 'c' วิธีจับคู่กลับกับ ('a') ตัดขึ้นในวัตถุ MatchData วิธีการสแกนช่วยให้การสแกนตามข้อความ และส่งกลับค่าที่ตรงกันทั้งหมดที่พบเป็นองค์ประกอบในอาร์เรย์: match_scan.rb TESTSTR = "abc ไม่ cba" ("
--match--") ทำให้ b = /[abc]/.match (TESTSTR) #= > MatchData: ทำให้ "a" ("-สแกน-") เป็น = TESTSTR.scan(/[abc]/) #= > เรย์: ["a", "b", "c", "c", "b", "a"]
การแปล กรุณารอสักครู่..

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"]
การแปล กรุณารอสักครู่..

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