background preloader

Regex & Pattern Matching

Facebook Twitter

Ruby Regular Expressions. A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern. A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r as follows: Syntax: /pattern//pattern/im # option can be specified%r! /usr/local! # general delimited regular expression Example: #! This will produce the following result: Line1 contains Cats Regular-expression modifiers: Regular expression literals may include an optional modifier to control various aspects of matching.

Like string literals delimited with %Q, Ruby allows you to begin your regular expressions with %r followed by a delimiter of your choice. . # Following matches a single slash character, no escape required%r|/| # Flag characters are allowed with this syntax, too%r[</(.*)>]i Regular-expression patterns: Except for control characters, (+ ? Following table lists the regular expression syntax that is available in Ruby. #! Ruby Regular Expressions. Finding the first match String. =~(Regexp) returns the starting position of the first match or nil if no match was found: >> "123 456 789" =~ /\d+/=> 0 >> "abc def ghi" =~ /\d+/=> nil >> "found" if "123 456 789" =~ /\d+/=> "found" Special $ variables will contain information about the last match: >> "123 456 789" =~ /\d\d\d/=> 0 # $` contains text before last match# $& contains last matched string# $' contains text after last match>> $` + '[' + $& + ']' + $'=> "[123] 456 789" Accessing captures $n contains the n-th (...) capture of the last match, $~ contains MatchData object: >> "123 456 789" =~ /(\d\d)(\d)/ >> [$1, $2]=> ["12", "3"] >> $~.captures=> ["12", "3"] Finding all matches String.scan returns all matches as String array: String.scan can also be used with a block: Replacing matches String.gsub returns a new String with matches replaced, String.gsub!

Modifiers /. Regular expressions by example /a/ character 'a' /\// character '/' (/\? More Information. Ruby Regex Examples | LZone. This post gives some simple examples for using regular expressions in Ruby scripts. 1. Syntax Compared to other scripting languages Ruby more behaves like Perl allowing to use regex seemlessly without the need for explicit objects in Python or a function in PHP.

So you can just match something with putting a regular expression between two slashes: 2. Here are some syntax examples that check strings for certain content: Basic Matching Using different regex delimiters Changing the delimiter becomes useful in some cases Case sensitity Matching with wildcards Using quantifiers Replacing Patterns You need to do substitution using the in place string object methods sub! Extracting Data with Ruby Regex To extract data using regular expression we have to use capture/grouping syntax and to do exactly one match: the String#match method and MatchData#captures to produce a result arrayto do multiple matches: the String#scan method which returns a nested array with an result array for each match.

Scan (String. Scan(p1)public Both forms iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block. If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is itself an array containing one entry per group. a = "cruel world"a.scan(/\w+/) a.scan(/.../) a.scan(/(...)/) a.scan(/(..)(..)/) And the block form: a.scan(/\w+/) {|w| print "<<#{w}>> " }print "\n"a.scan(/(.)(.)/) {|x,y| print y, x }print "\n" produces: <<cruel>><<world>> rceu lowlr Show source static VALUErb_str_scan(VALUE str, VALUE pat){ VALUE result; long start = 0; long last = -1, prev = 0; char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str); pat = get_pat(pat, 1); if (!

Pattern Matching.