CSC/ECE 517 Summer 2008/wiki1 1 mb: Difference between revisions
Line 31: | Line 31: | ||
=== Code Examples === | === Code Examples === | ||
'''Example 1:''' Examine a string and replace " | '''Example 1:''' Examine a string and replace any moo sound with moo. A moo sound is a word that begins with "moo" and may have more "o"'s on the end of it. These are valid moo sounds: moo, moooo, mooooooo | ||
'''Java:''' <br/> | '''Java:''' <br/> | ||
String s = | String s = “Cows say moooooo, when they are tired they say moooo”; <br/> | ||
Pattern p = Pattern.compile(" | Pattern p = Pattern.compile("mo{2,}"); <br/> | ||
Matcher m = p.matcher(s); <br/> | Matcher m = p.matcher(s); <br/> | ||
s = m.replaceAll( | s = m.replaceAll(“moo”); <br/> | ||
'''Ruby:''' <br/> | '''Ruby:''' <br/> | ||
s = | s = “Cows say moooooo, when they are tired they say moooo” <br/> | ||
e = Regexp.new(/ | e = Regexp.new(/mo{2,}/) <br/> | ||
s = s.gsub(e, | s = s.gsub(e, “moo”) <br/> | ||
The resulting string for both Java and Ruby will now be: <br/> | |||
Cows say moo, when they are tired the say moo. | |||
=== Other useful links === | === Other useful links === |
Revision as of 02:54, 7 June 2008
Regular expression support in Java and Ruby
What are regular expressions?
Regular expressions provide an efficient way to match patterns against strings. Both Java and Ruby use a regular expression syntax similar to Perl.
Perl regular expression syntax
How regular expressions are handled in Java
In Java, regular expressions are supported using the java.util.regex package.
Package summary for java.util.regex
The java.util.regex package contains two classes:
The Pattern class and
the Matcher class.
Instances of the Pattern class are used to represent regular expressions. Instances of the Matcher class are used to match the regular expressions described by the Pattern object against strings. The Pattern class provides methods to facilitate splitting strings based upon provided regular expressions, while the Matcher class provides methods that allow patterns to be found, replaced, and examined further.
How regular expressions are handled in Ruby
In Ruby, regular expressions are supported via the Regexp class.
Class summary for Regexp
The match and last_match methods of the Regexp class return MatchData objects.
Class summary for MatchData
Similar to the Pattern class in Java, instances of the Regexp class are used to represent regular expressions in Ruby. The methods of the Regexp class provide functionality that is comparable to the functionality provided by Java's Pattern class, such as splitting strings and testing for the existence of matches. The more sophisticated operations applicable to regular expressions are handled by the methods in the MatchData class.
Code Examples
Example 1: Examine a string and replace any moo sound with moo. A moo sound is a word that begins with "moo" and may have more "o"'s on the end of it. These are valid moo sounds: moo, moooo, mooooooo
Java:
String s = “Cows say moooooo, when they are tired they say moooo”;
Pattern p = Pattern.compile("mo{2,}");
Matcher m = p.matcher(s);
s = m.replaceAll(“moo”);
Ruby:
s = “Cows say moooooo, when they are tired they say moooo”
e = Regexp.new(/mo{2,}/)
s = s.gsub(e, “moo”)
The resulting string for both Java and Ruby will now be:
Cows say moo, when they are tired the say moo.
Other useful links
Basic regular expression syntax
Advanced regular expression syntax
java.util.regex Examples from The Java Developers Almanac 1.4