Regular Expressions and Patterns

A Regular Expression or Pattern matches custom rules applied to strings to tell you if it contains all of those things.  It is an expression used to specify a set of strings required for a particular purpose.  A Regular Expression lets you specify a pattern of characters to be matched in a string.  A simple way to specify a set of strings is simply to list its elements or members.

For example:

Handel

Händel

Haendel

could be specificed by pattern H(ä|ae?)ndel; we say that this pattern matches each of the three strings. 

gray | grey

or 

gr(a|e)y

These are equivalent patterns.

In Ruby, you typically create a regular expression by writing /pattern/ or %{pattern}.  

if line =~ /H(alä)ndel/

puts „No matter how you spell Handel, you’ve gotta love him!“

 

end

| Back

Footerfull