THE BOOK OF RUBY286groups.rb /(hi).*(h...o)/ =~

THE BOOK OF RUBY286groups.rb /(hi).

THE BOOK OF RUBY
286
groups.rb /(hi).*(h...o)/ =~ "The word 'hi' is short for 'hello'." After evaluating groups in a regular expression, a number of variables, equal to the number of groups, will be assigned the matched value of those groups. These variables take the form of a $ followed by a number: $1, $2, $3 and so on. After executing the above code, I can access the variables, $1 and $2 like this: print( $1, " ", $2, "
" ) #=> displays: hi hello Note that if the entire regular expression is unmatched, none of the group variables with be initialized. If, for example, ‘hi’ is in the string but ‘hello’ isn’t, neither of the group variables will be initialized. Both would be nil. Here is another example, which returns three groups, each of which contains a single character (given by the dot). Groups $1 and $3 are then displayed: /(.)(.)(.)/ =~ "abcdef" print( $1, " ", $3, "
" ) #=> displays: a c Here is a new version of the comment-matching program which was given earlier (regex3a.rb); this has now been adapted to use the value of the group (*.) which returns all the characters (zero or more) following the string matched by the preceding part of the regular expression (which here is: ^s*#). This matches zero or more whitespace (s*) characters from the start of the current line (^) up to the first occurrence of a hash or pound character: #: regex3b.rb File.foreach( 'regex1.rb' ){ |line| if line =~ /^s*#(.*)/ then puts( $1 ) end } The end result of this is that only lines in which the first printable character is # are matched; and $1 prints out the text of those lines minus the # character itself. As we shall see shortly, this simple technique provides the basis for a useful tool for extracting documentation from a Ruby file.
0/5000
จาก: -
เป็น: -
ผลลัพธ์ (ไทย) 1: [สำเนา]
คัดลอก!
หนังสือของทับทิม286groups.rb /(hi).*(h...o)/ =~ "The word 'hi' is short for 'hello'." After evaluating groups in a regular expression, a number of variables, equal to the number of groups, will be assigned the matched value of those groups. These variables take the form of a $ followed by a number: $1, $2, $3 and so on. After executing the above code, I can access the variables, $1 and $2 like this: print( $1, " ", $2, "
" ) #=> displays: hi hello Note that if the entire regular expression is unmatched, none of the group variables with be initialized. If, for example, ‘hi’ is in the string but ‘hello’ isn’t, neither of the group variables will be initialized. Both would be nil. Here is another example, which returns three groups, each of which contains a single character (given by the dot). Groups $1 and $3 are then displayed: /(.)(.)(.)/ =~ "abcdef" print( $1, " ", $3, "
" ) #=> displays: a c Here is a new version of the comment-matching program which was given earlier (regex3a.rb); this has now been adapted to use the value of the group (*.) which returns all the characters (zero or more) following the string matched by the preceding part of the regular expression (which here is: ^s*#). This matches zero or more whitespace (s*) characters from the start of the current line (^) up to the first occurrence of a hash or pound character: #: regex3b.rb File.foreach( 'regex1.rb' ){ |line| if line =~ /^s*#(.*)/ then puts( $1 ) end } The end result of this is that only lines in which the first printable character is # are matched; and $1 prints out the text of those lines minus the # character itself. As we shall see shortly, this simple technique provides the basis for a useful tool for extracting documentation from a Ruby file.
การแปล กรุณารอสักครู่..
ผลลัพธ์ (ไทย) 2:[สำเนา]
คัดลอก!
หนังสือของทับทิม
286
groups.rb /(hi).*(h...o)/ = ~ "คำว่า 'สวัสดี' สั้นสำหรับ 'สวัสดี'." หลังจากการประเมินในกลุ่มแสดงออกปกติจำนวนของตัวแปรเท่ากับจำนวนของกลุ่มที่จะได้รับมอบหมายค่าจับคู่ของกลุ่มเหล่านั้น ตัวแปรเหล่านี้จะอยู่ในรูปของดอลลาร์ตามด้วยหมายเลข: $ 1, $ 2, $ 3 และอื่น ๆ หลังจากรันโค้ดข้างต้นฉันสามารถเข้าถึงตัวแปร $ 1 และ 2 $ เช่นนี้พิมพ์ ($ 1, "", $ 2, " n") # => แสดง: สวัสดีสวัสดีทราบว่าหากการแสดงออกปกติทั้งหมดจะไม่มีที่เปรียบไม่ ของกลุ่มที่มีตัวแปรสามารถเริ่มต้น ตัวอย่างเช่นหาก 'สวัสดี' อยู่ในสตริง แต่ 'สวัสดี' ไม่ได้ทั้งตัวแปรกลุ่มจะมีการเริ่มต้น ทั้งสองจะเป็นศูนย์ นี่เป็นอีกตัวอย่างหนึ่งซึ่งผลตอบแทนสามกลุ่มซึ่งแต่ละที่มีตัวเดียว (ที่ได้รับจากจุด) กลุ่ม 1 $ และ $ 3 แล้วแสดง: /(.)(.)(.)/ = ~ "abcdef" พิมพ์ ($ 1, "", $ 3, " n") # => แสดง: AC นี่รุ่นใหม่เป็น โปรแกรมความคิดเห็นที่ตรงกับที่ได้รับก่อนหน้านี้ (regex3a.rb); นี้ได้รับการปรับให้ใช้ค่าของกลุ่มซึ่งผลตอบแทนทุกตัวอักษร (ศูนย์หรือมากกว่า) ต่อไปนี้สตริงการจับคู่โดยส่วนก่อนหน้าของการแสดงออกปกติ (*.) (ซึ่งในที่นี้คือ ^ s * #) นี้ตรงกับศูนย์หรือมากกว่าช่องว่าง ( s *) ตัวละครจากจุดเริ่มต้นของบรรทัดปัจจุบัน (^) ขึ้นไปเกิดขึ้นครั้งแรกของกัญชาหรือปอนด์ตัวอักษร: #: regex3b.rb ​​File.foreach ('regex1.rb') { | สาย | หากสาย = ~ /^s*#(.*)/ แล้วทำให้ ($ 1)} สิ้นผลในตอนท้ายของเรื่องนี้ก็คือเส้นเดียวที่อยู่ในตัวอักษรที่พิมพ์ครั้งแรกคือ # มีการจับคู่; และ $ 1 พิมพ์ออกข้อความของเส้นที่ลบตัวอักษร # ตัวเอง ในฐานะที่เราจะได้เห็นในไม่ช้านี้เทคนิคง่ายๆให้พื้นฐานสำหรับเครื่องมือที่มีประโยชน์สำหรับการแยกเอกสารจากแฟ้มทับทิม
การแปล กรุณารอสักครู่..
ผลลัพธ์ (ไทย) 3:[สำเนา]
คัดลอก!
หนังสือของทับทิม
0
groups.rb / ( HI ) * ( . . . . . o ) = ~ " คำว่า ' สวัสดี ' สั้น ' ฮัลโหล ' " หลังจากประเมินกลุ่มในการแสดงออกปกติ ตัวเลขของตัวแปรเท่ากับจำนวนกลุ่มจะมอบหมายให้ตรงกับค่าของกลุ่ม เหล่านั้น ตัวแปรที่ใช้รูปแบบของ $ ตามด้วยหมายเลข : $ 1 , $ 2 , $ 300 และอื่น ๆ หลังจากรันโค้ดข้างต้น , ฉันสามารถเข้าถึงตัวแปร$ 1 และ $ 2 อย่างนี้ : พิมพ์ ( $ 1 , $ 2 , " " , " n " ) # = > แสดง : สวัสดีสวัสดีทราบว่าถ้าการแสดงออกปกติทั้งหมดไม่ตรงกัน ไม่มีตัวแปรกลุ่มสามารถเริ่มต้น . ตัวอย่างเช่นถ้า ' สวัสดี ' แต่ ' ฮัลโหล ' ในข้อความไม่ได้ หรือตัวแปรของกลุ่มจะสามารถเริ่มต้น . ก็จะไม่เกิดขึ้น นี่เป็นอีกตัวอย่างที่ 4 สามกลุ่มแต่ละแห่งซึ่งประกอบด้วยตัวอักษรเดียว ( กำหนดโดยจุด ) กลุ่มที่ 1 $ และ $ 3 จะแสดง : / ( . ) ( . ) ( . ) = ~ " abcdef " พิมพ์ ( $ 1 , $ 3 , " " , " n " ) # = > แสดง : C นี่เป็นรุ่นใหม่ของความคิดเห็นที่ตรงกัน โปรแกรมที่ได้รับก่อนหน้านี้ ( regex3a . RB ) ; นี้ได้ถูกดัดแปลงให้ใช้ค่าของกลุ่ม ( * .) ซึ่งผลตอบแทนทุกตัวอักษร ( ศูนย์หรือมากกว่า ) ต่อสายตรงกันโดยก่อนหน้านี้เป็นส่วนหนึ่งของการแสดงผลปกติ ( ซึ่งที่นี่ :
s * # ) นี้ตรงกับศูนย์หรือมากกว่าช่องว่าง ( s * ) อักขระจากจุดเริ่มต้นของบรรทัดปัจจุบัน (
) ถึงการปรากฏครั้งแรกของกัญชาหรือปอนด์#ตัวละคร : : ไฟล์ regex3b.rb . foreach ( ' regex1 . RB ' ) { | สาย | ถ้าบรรทัด = ~ /
s * # ( .* ) แล้วใส่ ( $ 1 ) จบ } สิ้นผลนี้เป็นเพียงสายที่ตัวอักษรที่พิมพ์ครั้งแรก#ตรง ; และ $ 1 ออกพิมพ์ข้อความบรรทัดนั้นลบ#ตัวละครนั่นเอง ที่เราจะเห็นในไม่ช้า เทคนิคง่าย ๆนี้จะเป็นพื้นฐานสำหรับเป็นเครื่องมือที่มีประโยชน์สำหรับการแยกเอกสารจากแฟ้มทับทิม
การแปล กรุณารอสักครู่..
 
ภาษาอื่น ๆ
การสนับสนุนเครื่องมือแปลภาษา: กรีก, กันนาดา, กาลิเชียน, คลิงออน, คอร์สิกา, คาซัค, คาตาลัน, คินยารวันดา, คีร์กิซ, คุชราต, จอร์เจีย, จีน, จีนดั้งเดิม, ชวา, ชิเชวา, ซามัว, ซีบัวโน, ซุนดา, ซูลู, ญี่ปุ่น, ดัตช์, ตรวจหาภาษา, ตุรกี, ทมิฬ, ทาจิก, ทาทาร์, นอร์เวย์, บอสเนีย, บัลแกเรีย, บาสก์, ปัญจาป, ฝรั่งเศส, พาชตู, ฟริเชียน, ฟินแลนด์, ฟิลิปปินส์, ภาษาอินโดนีเซี, มองโกเลีย, มัลทีส, มาซีโดเนีย, มาราฐี, มาลากาซี, มาลายาลัม, มาเลย์, ม้ง, ยิดดิช, ยูเครน, รัสเซีย, ละติน, ลักเซมเบิร์ก, ลัตเวีย, ลาว, ลิทัวเนีย, สวาฮิลี, สวีเดน, สิงหล, สินธี, สเปน, สโลวัก, สโลวีเนีย, อังกฤษ, อัมฮาริก, อาร์เซอร์ไบจัน, อาร์เมเนีย, อาหรับ, อิกโบ, อิตาลี, อุยกูร์, อุสเบกิสถาน, อูรดู, ฮังการี, ฮัวซา, ฮาวาย, ฮินดี, ฮีบรู, เกลิกสกอต, เกาหลี, เขมร, เคิร์ด, เช็ก, เซอร์เบียน, เซโซโท, เดนมาร์ก, เตลูกู, เติร์กเมน, เนปาล, เบงกอล, เบลารุส, เปอร์เซีย, เมารี, เมียนมา (พม่า), เยอรมัน, เวลส์, เวียดนาม, เอสเปอแรนโต, เอสโทเนีย, เฮติครีโอล, แอฟริกา, แอลเบเนีย, โคซา, โครเอเชีย, โชนา, โซมาลี, โปรตุเกส, โปแลนด์, โยรูบา, โรมาเนีย, โอเดีย (โอริยา), ไทย, ไอซ์แลนด์, ไอร์แลนด์, การแปลภาษา.

Copyright ©2024 I Love Translation. All reserved.

E-mail: