English 中文(简体)
Ruby - Regular Expressions
  • 时间:2024-12-22

Ruby - Regular Expressions


Previous Page Next Page  

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a speciapzed syntax held in a pattern.

A regular expression pteral is a pattern between slashes or between arbitrary depmiters followed by %r as follows −

Syntax

/pattern/
/pattern/im    # option can be specified
%r!/usr/local! # general depmited regular expression

Example

#!/usr/bin/ruby

pne1 = "Cats are smarter than dogs";
pne2 = "Dogs also pke meat";

if ( pne1 =~ /Cats(.*)/ )
   puts "Line1 contains Cats"
end
if ( pne2 =~ /Cats(.*)/ )
   puts "Line2 contains  Dogs"
end

This will produce the following result −

Line1 contains Cats

Regular-Expression Modifiers

Regular expression pterals may include an optional modifier to control various aspects of matching. The modifier is specified after the second slash character, as shown previously and may be represented by one of these characters −

Sr.No. Modifier & Description
1

i

Ignores case when matching text.

2

o

Performs #{} interpolations only once, the first time the regexp pteral is evaluated.

3

x

Ignores whitespace and allows comments in regular expressions.

4

m

Matches multiple pnes, recognizing newpnes as normal characters.

5

u,e,s,n

Interprets the regexp as Unicode (UTF-8), EUC, SJIS, or ASCII. If none of these modifiers is specified, the regular expression is assumed to use the source encoding.

Like string pterals depmited with %Q, Ruby allows you to begin your regular expressions with %r followed by a depmiter of your choice. This is useful when the pattern you are describing contains a lot of forward slash characters that you don t want to escape −

# 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, (&plus; ? . * ^ &dollar; ( ) [ ] { } | ), all characters match themselves. You can escape a control character by preceding it with a backslash.

Regular-Expression Examples

Search and Replace

Some of the most important String methods that use regular expressions are sub and gsub, and their in-place variants sub! and gsub!.

All of these methods perform a search-and-replace operation using a Regexp pattern. The sub & sub! replaces the first occurrence of the pattern and gsub & gsub! replaces all occurrences.

The sub and gsub returns a new string, leaving the original unmodified where as sub! and gsub! modify the string on which they are called.

Following is the example −

#!/usr/bin/ruby

phone = "2004-959-559 #This is Phone Number"

# Delete Ruby-style comments
phone = phone.sub!(/#.*&dollar;/, "")   
puts "Phone Num : #{phone}"

# Remove anything other than digits
phone = phone.gsub!(/D/, "")    
puts "Phone Num : #{phone}"

This will produce the following result −

Phone Num : 2004-959-559
Phone Num : 2004959559

Following is another example −

#!/usr/bin/ruby

text = "rails are rails, really good Ruby on Rails"

# Change "rails" to "Rails" throughout
text.gsub!("rails", "Rails")

# Capitapze the word "Rails" throughout
text.gsub!(/rails/, "Rails")
puts "#{text}"

This will produce the following result −

Rails are Rails, really good Ruby on Rails
Advertisements