-
-
Save hayeah/7676398 to your computer and use it in GitHub Desktop.
What a template might look like
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Creating and Using Regexps | |
| You're reading this because you want to use regular expressions when you code. | |
| So this is probably a good time to tell you how to represent and use regular | |
| expressions in your programs. | |
| The good news is that most languages make it easy to use regular expressions. | |
| The bad news is that each language does it differently. | |
| {{#margin id="babel-tower"}} | |
| {{img "images/iStock_000005187601Large-babel.png"}} | |
| The Tower of Babel | |
| {{/margin}} | |
| There is however a lot in common across all languages. They all have you | |
| create something representing a regular expression. (In object-oriented | |
| languages, this will be an instance of some kind of `Regular Expression` | |
| class.) You can then call methods to that object to apply it to a string. You | |
| may also, depending on the language, be able to do the opposite and ask a | |
| string object whether it is matched by a regexp. | |
| To see how to represent and use regexps in some common languages, push the | |
| corresponding magnifying glass icon in the list that follows. | |
| + Ruby (using explicit classes and methods) {{aside ref="ruby-explicit"}} | |
| + Ruby (using shortcut syntax) {{aside ref="ruby-shortcut"}} | |
| + Python {{aside ref="python"}} | |
| + Java | |
| + Javascript | |
| {{#aside id="ruby-explicit"}} | |
| Ruby regular expressions are objects of type `Regexp`. These objects support a | |
| rich set of methods. The basic one is `match`, which applies the pattern to a | |
| string and returns either an object describing the match (of type `MatchData`) | |
| or `nil` if there is no match. | |
| {{#code lang="ruby"}} | |
| regexp = Regexp.new("ant") | |
| match = regexp.match("pants") | |
| {{/code}} | |
| {{/aside}} | |
| {{#aside id="ruby-shortcut"}} | |
| Ruby also supports regular expressions directly in the language's syntax. | |
| `/pattern/` is a regular expression literal. It creates a `Regexp` object | |
| (just as `"string"` literals create `String` objects). The `=~` operator | |
| matches a string and a regexp, returning the offset of the match if there is | |
| one, or `nil` otherwise. | |
| {{#code lang="ruby"}} | |
| regexp = Regexp.new("ant") | |
| match = regexp.match("pants") | |
| {{/code}} | |
| {{/aside}} | |
| {{#aside id="python"}} | |
| Regular expressions in Python are implemented in the `re` package. You pass | |
| the pattern as a string to functions like `re.compile()` and `re.search()`. As | |
| we'll see later in this course, patterns often include characters such as | |
| backslash (`\`) which have special meaning in string literals. In Python, you | |
| turn off this special meaning by prefixing the literal with a lowercase `r` | |
| (making it a *raw* string). So, in the following examples, `r'ant'` is | |
| simply the string `a`-`n`-`t`. The `re` package converts this string into a | |
| meaningful regexp. | |
| {{#code lang="ruby"}} | |
| import re | |
| # create a regexp from a pattern, and then | |
| # match that pattern against a string | |
| regexp = re.compile(r'ant') | |
| print regexp.search("pants") # => Match object | |
| # or use the shortcut form... | |
| print re.search(r'ant', "pants") | |
| regexp = Regexp.new("ant") | |
| match = regexp.match("pants") | |
| {{/code}} | |
| {{/aside}} | |
| <section> | |
| ## Some Real World Examples | |
| Remember that we said that a regular expression is a pattern, and we represent that pattern using the characters available on our keyboard. So let's see how that works in practice. | |
| {{#pref "lang"}} | |
| If you'd like, we can set a prefered programming language for the rest of the | |
| examples in this course. You can always reset this using the <em>Spanner</em> | |
| link (<icon class="icon-wrench"/>) in the footer of every page. | |
| {{/pref}} | |
| </section> | |
| {{#ifpref "lang" "ruby"}} | |
| {{include "_representing-regexp-ruby-examples.hbs.html"}} | |
| {{/ifpref}} | |
| {{#ifpref "lang" "python"}} | |
| {{include "_representing-regexp-python-examples.hbs.html"}} | |
| {{/ifpref}} | |
| {{#ifpref "lang" "javascript"}} | |
| {{include "_representing-regexp-javascript-examples.hbs.html"}} | |
| {{/ifpref}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment