Regular Expressions

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

ruby มีเครื่องหมาย =~ เอาไว้เทียบ regular expression กับ String

/old/ =~ 'this old house' # return 5

เป็นตำแหน่งของ old ใน ‘this old house’

/Russ|Russell/ =~ 'Fred' # return nil

เพราะใน Fred ไม่มี Russ หรือ Russell

/.*/ =~ 'any old string' # return 0

เพราะ .* หมายถึง string ใดๆ

เราสามารถเอา =~ ไปใช้ในประโยคเงื่อนไข

if /Russ|Russell/ =~ s
  puts "Russ or Russell"
end

หรือใช้เพื่อกำหนดค่าให้ตัวแปล

x = /old/ =~ 'this old house'

สำหรับการใช้สุดยอดเครื่องมืออย่าง regular expression ลองอ่านใน wiki pedia หรือลองเล่มนี้ครับ Mastering Regular Experession

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น