regex - Basic Matching in Ruby -


i working through book , gives example

x = "this test".match(/(\w+) (\w+)/) 

we looking @ parentheses , being able access passed separately.

when put expression above irb get:

matchdata "this is" 1:"this" 2:"is">

why doesn't include a , test?

would have include .match(/(\w+) (\w+) (\w+) (\w+)/) ?

the 'match' method not matching regex globally. returning first match. can use 'scan' method rather 'match' , should return array of matches of regex.

[~]$ irb 1.8.7-p371 :001 > x = "this test".match(/(\w+) (\w+)/)  => #<matchdata "this is" 1:"this" 2:"is"> 1.8.7-p371 :002 > x = "this test".scan(/(\w+) (\w+)/)  => [["this", "is"], ["a", "test"]] 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -