CSC/ECE 517 Fall 2009/wiki1b 5 j8: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 17: Line 17:
     Pattern patt = Pattern.compile("l.*");
     Pattern patt = Pattern.compile("l.*");
     Matcher match = patt.matcher("lee");
     Matcher match = patt.matcher("lee");
     match.matches();
     return match.matches();


This would return true since 'lee' starts with an 'l' and has zero or more characters after it.  Since the Pattern object is created with the regular expression, it can be reused with different inputs for increased speed.
This would return true since 'lee' starts with an 'l' and has zero or more characters after it.  Since the Pattern object is created with the regular expression, it can be reused with different inputs for increased speed.
Line 24: Line 24:
     Pattern patt = Pattern.compile("l.*");
     Pattern patt = Pattern.compile("l.*");
     Matcher match = patt.matcher("eel");
     Matcher match = patt.matcher("eel");
     match.matches();
     return match.matches();


This would return false since 'eel' does not start with an 'l'.
This would return false since 'eel' does not start with an 'l'. If a developer simply wants to a regular expression once and does not care to reuse the Pattern, he or she can simply use the 'matches' static method within Patthern:
 
    Pattern.matches("l.*", "lee");


===Ruby===
===Ruby===

Revision as of 15:06, 20 September 2009

Regular Expressions

Who Compare the support for regular expressions in Ruby, Python, PHP, Perl 5, and any other appropriate scripting language with each other. Also compare the syntactic features in these languages with Java's package-based support. What features and syntax do the languages have in common? Are some features supported by some languages and not by others? How robust and easy-to-use are regular expressions in all these languages?

Regular expressions are a critical part of most modern programming languages especially ones that deal string processing as a core part of their functionality. Although using regular expressions can change from language to language, the general principle is the same and similar syntax can generally used across the board.


Usage

Perl

Java

Unlike many languages Java does not have built-in language support for regular expressions. It instead uses Pattern objects to process regular expressions.

    Pattern patt = Pattern.compile("l.*");
    Matcher match = patt.matcher("lee");
    return match.matches();

This would return true since 'lee' starts with an 'l' and has zero or more characters after it. Since the Pattern object is created with the regular expression, it can be reused with different inputs for increased speed.


    Pattern patt = Pattern.compile("l.*");
    Matcher match = patt.matcher("eel");
    return match.matches();

This would return false since 'eel' does not start with an 'l'. If a developer simply wants to a regular expression once and does not care to reuse the Pattern, he or she can simply use the 'matches' static method within Patthern:

   Pattern.matches("l.*", "lee");

Ruby

Python

Php