background preloader

Regular Expressions

Facebook Twitter

Ruby Regexp

Regexp. A Regexp holds a regular expression, used to match a pattern against strings.

Regexp

Regexps are created using the /.../ and %r{...} literals, and by the Regexp::new constructor. Regular expressions (regexps) are patterns which describe the contents of a string. They’re used for testing whether a string contains a given pattern, or extracting the portions that match. They are created with the /pat/ and %r{pat} literals or the Regexp.new constructor. A regexp is usually delimited with forward slashes (/). /hay/ =~ 'haystack' /y/.match('haystack') If a string contains the pattern it is said to match. /needle/.match('haystack') /hay/.match('haystack') Specifically, /st/ requires that the string contains the letter s followed by the letter t, so it matches haystack, also. The following are metacharacters (, ), [, ], {, }, ., ? /1 \+ 2 = 3\? Patterns behave like double-quoted strings so can contain the same backslash escapes. /\s\u{6771 4eac 90fd}/.match("Go to 東京都") Character Classes¶ ↑ Repetition¶ ↑

Regular expression. The regular expression(?

Regular expression

<=\.) {2,}(? =[A-Z]) matches at least two spaces occurring after period (.) and before an upper case letter as highlighted in the text above. Each character in a regular expression is either understood to be a metacharacter with its special meaning, or a regular character with its literal meaning. Together, they can be used to identify textual material of a given pattern, or process a number of instances of it that can vary from a precise equality to a very general similarity of the pattern.

The pattern sequence itself is an expression that is a statement in a language designed specifically to represent prescribed targets in the most concise and flexible way to direct the automation of text processing of general text files, specific textual forms, or of random input strings. History[edit] Regular expressions originated in 1956, when mathematician Stephen Cole Kleene described regular languages using his mathematical notation called regular sets. Basic concepts[edit] Regex Tutorial—Strategies to write Good Regex. Knowing the English alphabet doesn't make you Hemingway.

Regex Tutorial—Strategies to write Good Regex

Likewise, knowing regex syntax doesn't make you literate with regex. Style is a hard thing to teach aspiring writers, but the rudiments of style can certainly be taught. Likewise, "good regex" is not easy to put into rules, and perhaps that's why I've never seen literature on the subject. That makes it all the more interesting to try. With the disclaimer that I do not present myself as an authority on efficient, graceful regex, here are some considerations that have crossed my mind. Should I Match it, or Should I Capture it? Matches are not sacred. One question I've never seen discussed (but maybe I'm not reading widely and carefully enough) is that of the various strategies available in order to retrieve the data you need. In the examples on this site, you'll see a diverse use of matching and capturing.

Sometimes, then, the captured groups contain the data we're after. If this sounds confusing, it doesn't need to be. Regex Tutorial—Regex Tricks, Traps and Optimizations. The best regex trick or optimization, in my opinion, is to start with good style.

Regex Tutorial—Regex Tricks, Traps and Optimizations

So I highly recommend you visit the page on regex style if you haven't already done so. There, among other things, you will find a mnemonic phrase that will help you polish off many of your expressions: greedy atoms anchor again. This page is contains two major sections: ✽ Regex Optimizations ✽ Regex Tricks and Traps Optimizing Your Regular Expressions Suppose you travel to an exotic country. Regex Tutorial—Regular Expressions have Many Uses. Regex is the gift that keeps giving.

Regex Tutorial—Regular Expressions have Many Uses

Once you learn it, you discover it comes in handy in many places where you hadn't planned to use it. On this page, we'll first look at a number of contexts and programs where you may find regex. Then we'll have a quick look at some regex flavors you may run into. Finally, we'll study some examples of regex patterns in contexts such as: ✽ File Renaming ✽ Text Search ✽ Web directives (Apache) ✽ Database queries (MySQL) Situations where Regex can Save the Day Here are some of the thing regular expressions can help you do. 1. 2. A few words about the tools just mentioned. For replacement in text files, I love ABA Replace. I use regex to rename files, to search in files, to make large-scale substitutions in code, in code (PHP), with databases (mySQL) and to direct my web server (Apache). 3. 4. Regex Tutorial—Regex Lookarounds: Lookahead and Lookbehind.

Lookarounds often cause confusion to the regex apprentice.

Regex Tutorial—Regex Lookarounds: Lookahead and Lookbehind

I believe this confusion promptly disappears if one simple point is firmly grasped. It is that at the end of a lookahead or a lookbehind, the regex engine hasn't moved on the string. You can chain three more lookaheads after the first, and the regex engine still won't move. In fact, that's a useful technique. A quick syntax reminder This page digs deep into the details of lookahead and lookbehind and assumes you've already become familiar with the basic syntax, perhaps by reading the lookaround section of the reference on (? (direct link)Jumping Points For easy navigation, here are some jumping points to various sections of the page: