Ruby

26
1359 reads

February 28, 2013

Matchdata


A MatchData object may contain groups or ‘captures’ and these can be returned in an array using either the to_a or captures method, like the following Program

22
1487 reads

February 28, 2013

String Method Match Scan


Up to now, we’ve used methods of the Regexp class when processing strings.

21
1305 reads

February 28, 2013

Match Groups


When using match with groups, you can use array-style indexing to obtain specific items. Index 0 is the original string; higher indexes are the groups

25
1433 reads

February 28, 2013

Match Data


The =~ ‘operator’ is not the only means of finding a match. The Regexp class also has a match method.

31
1310 reads

February 28, 2013

Test a Match


To test for a match you can use if and the =~ operator

35
1487 reads

February 28, 2013

Match Groups


You can also use a regular expression to match one or more substrings. In order to do this, you should put part of the regular expression between round brackets.

35
1371 reads

February 28, 2013

Greedy Matching2


You can control the greediness of pattern matching to do things such as process directory paths

22
1249 reads

February 28, 2013

Greedy Matching


When a string contains more than one potential match, you may sometimes want to return the string up to the first match (that is, as little of the string as possible consistent with the match pattern)

28
1399 reads

February 28, 2013

Test Module


Any code which would normally be executed when a file is run will be executed when that file is required

40
1559 reads

February 28, 2013

Test


The file loaded does not introduce the new namespace into the main program and you will not have access to all the module(s) in the loaded file.