Source From Here
I’m going to break it down, step by step, and take you through the concepts of regular expressions in Ruby and on to advanced techniques. My hope is that you’ll see the beauty of regular expressions and be able to move beyond the fear and intimidation and embrace them in your code.
A regular expression is just a pattern. It’s a pattern that a string either fits or it doesn’t. Programming Ruby 1.9 by Dave Thomas (more commonly known as the pickaxe book) sums up what you do with regular expressions in three words--test, extract, change. You can test a string to see if it matches your pattern. You can extract a string (or part of a string) that matches your pattern. And you can change a string, replacing parts that match your pattern with other text. Test, extract, change. So simple, yet so powerful.
Regular Expressions in Ruby
Ruby lets us take regular expressions to further heights. In Ruby, everything is an object. This includes regular expressions. Since you can send messages to objects, you can also send messages to regular expressions. You can also assign them to variables, pass them to methods, and more.
As of Ruby 1.9, Ruby uses the Oniguruma regular expressions library. It provides all the standard regular expressions features that of other Regular regular Expressions expressions library, but adds additional features and extensions. It handles complex, multi-byte characters (such as Japanese text) very well. A feature I particularly like is I can use /h and /H as shorthand for hexadecimal digits.
I recently found out Ruby 2.0 use a different regular expressions library, Onigmo. Onigmo is forked off of Oniguruma and will add even more features that can be harnessed by Ruby. It will be quite interesting to see what it brings. Regardless of what changes Onigmo makes, it won’t change the fundamental thing you do with a regular expression. You match your regular expression to a string. You compose a pattern and match a string to it.
In most of my Ruby regular expressions, I use the =~ operator. This is Ruby’s basic pattern matching operator. When I use this operator, I’m asking Ruby "Does this string contain this pattern?". Let’s go through an example:
On the left side I have a regular expression, which in this case is the literal word force. On the other side I have a quote from one of my very favorite movies, Star Wars. "Use the force." When I run this, I’m asking Ruby if my pattern, on the left, exists in the string on the right. What’s nice is I can format it the opposite way if I want to.
I can have the string on the left and the regular expression on the right. I’m running the same function, just in different words - does my string contain my regular expression? Some people find this a little more readable. When I run this, it returns the character number where my match starts. In this case, my match for the pattern /force/ begins on the eighth character of my string.
I can also test if a string DOES NOT match a pattern by using the operator !~. This just returns a Boolean of true or false.
Operators are great for basic matching, knowing whether my string matches my regular expression or does not. But Ruby provides me much more information about my match. All I have to do is ask.
The secret is to make my match a MatchData object. I create this object using the match method. Let’s say I have a string.
And I want to find out if this string contains the word force. I call the match method on my regular expression and pass it my string. When I run this, it returns an instance of the MatchData class for my particular match:
As of Ruby 1.9, I actually don’t have to start my match at the beginning of my string. I can pass a second argument, an integer, which means start my match on that character number.
In this case, it returns nil. In order to find a match for the word force, it needs to start matching earlier in the string. For this example, however, I’m just going to pass my string the first way. I have access to methods which give me MUCH more information about my match because my match is an instance of the MatchData class.
If I call to_s on my match, it will return the entire text of my match. If I call pre_match on my match, it will return the portion of my string that comes BEFORE my match:
If I call post_match on my match, it will return the portion of my string that comes AFTER my match:
All these methods (and there are several more) are quite useful. However, MatchData truly shines when it comes to capture groups, though. Capture groups are subexpressions within your regular expression. Let’s look at an example.
In order for a string to match this regular expression, it has to have any character appearing any number of times - that’s what the .* means- followed by the word force, followed by any character appearing any number of times. Notice that I have the first and last part of this expression in parentheses. These are called groups. When I run this match on my string, it will store matches for my groups in memory. I can access these groups and use the in other places in my code.
MatchData objects are a lot like arrays. I can access individual captures using bracket notation, similar to how I access individual elements of an array. If I runmy_match, I get my first capture group, "The ". Similarly, my_match returnd my second capture group, " will be with you always".
Notice I didn’t start with my_match, like I would with the first element of an array. If I run my_match, rather than getting my first match, I get the full string I ran the match on.
It’s important to remember that although MatchData objects are similar to Arrays, they’re not actually arrays. If I try to call an array method like each on my MatchDataobject, I get back a no method error. I can easily fix this, however, by converting my my MatchData to an array using the to_a method.
* Using Regular Expression in Ruby - Part2
* Using Regular Expression in Ruby - Part3
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- ActiveMQ In Action
- Big Data 研究
- Design Pattern
- Device Driver Programming
- Docker 工具
- Docker Practice
- English Writing
- ExtJS 3.x
- Git Pro
- Hadoop. Hadoop Ecosystem
- Java Framework
- Java UI
- Learn Spark
- ML Udemy
- node js
- Python Std Library
- Python tools
- Ruby Packages
- Windows 技巧
Preface In the last chapter we saw an iterator algebra that builds on the itertools module. In some ways, higher-order functions (...
Preface: 在這個階層中，我們只需考慮電路模組的功能，而不需考慮其硬體的詳細內容. Verilog 的時序控制為以事件為基礎的時序控制: * 接線或暫存器的值被改變。 * 模組的輸入埠接收到新的值 * 正規...
來源自 這裡 前言 : Thread 是 threading 模塊中最重要的類之一，可以使用它來創建線程。有兩種方式來創建線程：一種是通過繼承Thread 類，重寫它的 run 方法；另一種是創建一個 threading.Thread 對象，在它的初始化...
Source From Here Preface 本文實例講述了Python中 zip() 函數的定義及用法，相信對於Python初學者有一定的借鑒價值。 一、定義： zip([iterable, ...]) zip() 是 Python 的一個內建...