Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorStubblebine, Tony

A guide to the syntax and semantics of regular expressions for Perl 5.8, Ruby, Java, PHP, C#, .NET, Python, JavaScript, and PCRE.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Regular Expression Pocket Reference — Reading Guide ## 【One-Line Pitch】 A compact, language-by-language reference for anyone who needs to write or debug regular expressions across Perl, Ruby, PHP, Python, Java, C#, .NET, and JavaScript—ideal for programmers who switch languages and need quick syntax lookup without wading through a full textbook. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces what regular expressions are and establishes the core concepts of pattern matching, including the two engine types (DFA and NFA) that determine how regexes behave differently across languages. - **Early (~10%–23%)**: Covers the universal metacharacters, character representations (octal, hex, Unicode escapes), POSIX character classes, Unicode properties/scripts/blocks, anchors, and zero-width assertions—the shared vocabulary that appears in every language's implementation. - **Early (~23%–29%)**: Moves into practical recipes (trimming whitespace, validating numbers, URLs, currency) and then dives into Perl 5.8's rich regex feature set, including its operators, mode modifiers, grouping, capturing, and conditional patterns. - **Middle (~29%–48%)**: Surveys Java's java.util.regex package (Pattern/Matcher classes, Unicode support limitations) and .NET's System.Text.RegularExpressions module, covering their class hierarchies, methods, and language-specific syntax variations. - **Late (~48%–100%)**: Continues the language-by-language tour through PHP, Python, Ruby, and JavaScript, each with supported metacharacters, pattern-matching functions/methods, Unicode notes, and worked examples. ## 【Key Takeaways】 - **Two engine types explain most cross-language surprises** (Early): DFA engines guarantee linear-time matching but lack backreferences and lookarounds, while NFA engines (used by Perl, Java, .NET) support richer features but can backtrack catastrophically. Knowing which engine your language uses tells you what syntax is available and why patterns behave differently. - **Character escapes are the foundation of portable patterns** (Early): Octal (\015), hex (\x0D), and Unicode (\u0041, \x{...}) escapes let you match characters that are hard to type directly, but newline conventions vary by OS—\n is LF on Unix but CR on old Macs, so test carefully. - **\b is dangerously overloaded** (Early): It means "word boundary" outside character classes but "backspace" inside them—a classic source of bugs when patterns are reused in different contexts. - **Unicode support is inconsistent across languages** (Early): While \p{L}, \p{Nd}, and similar properties work broadly, Java's \w, \d, and \s only match ASCII by default, and block names require specific prefixes (In) and formatting rules—always verify what your target language actually supports. - **Capturing groups and backreferences are numbered by opening parentheses** (Early): \1, \2, etc. refer to earlier submatches, and non-capturing groups (?:...) let you group without cluttering your capture numbering—essential for complex patterns. - **Mode modifiers can be scoped** (Early): Flags like /i (case-insensitive), /m (multiline), /s (dot matches newline), and /x (free-spacing with comments) can be turned on/off for the whole pattern or just a subexpression using (?mode:...) syntax—a powerful way to localize behavior changes. - **Each language has its own API surface** (Middle): Java uses Pattern.compile() and Matcher methods (find, group, replaceAll), while .NET offers static and instance methods on Regex, and Perl provides operators like s/// and split—the regex syntax transfers, but the surrounding API does not. ## 【Reading Tips】 - **Use the opening 10% as your foundation**: The engine-type explanation and universal metacharacter tables are the most valuable conceptual content—read this section fully before anything else, as it explains why the same pattern can behave differently across languages. - **Treat the language chapters as lookup references, not sequential reading**: Skim the metacharacter tables for each language first, then jump to the "Examples" section to see realistic usage patterns—the examples (like the URL-to-HTML-link conversion in Java) show best practices for combining captures, lookaheads, and replacement strings. - **Pay special attention to the Unicode sections**: These are easy to skim past but contain critical gotchas—for instance, Java's \b and \B understand Unicode while \w and \d don't, and .NET's replacement string syntax ($1, $&, $`) differs from Perl's. - **The recipes section is a quick-win**: The early "Recipes" (whitespace trimming, number validation, URL matching) give you immediately usable patterns and show how to combine anchors, quantifiers, and character classes in practice. - **Don't expect deep theory**: This is a pocket reference, not a regex textbook—if you need to understand backtracking internals or optimization strategies in depth, the book points to Mastering Regular Expressions by Friedl as the companion resource. ## 【Coverage Limits】 This guide covers the book's structure and key concepts from the opening through the Java and .NET sections (~48% of the book). The PHP, Python, Ruby, and JavaScript chapters follow the same pattern but are not individually detailed here. ##
Page 5
registered trademarks of Microsoft Corporation. Spider-Man is a registered trademark of Marvel Enterprises, Inc. While every precaution has been taken in the...
View in text
Page 18
Some implementations support \A, which matches only at the beginning of the text. End of line/string: $, \Z, \z $ matches at the end of a string. In multilin...
View in text
Excerpt 3
ons in Java 1.5 and 1.6. Supported Metacharacters java.util.regex supports the metacharacters and metase- quences listed in Table 11 through Table 15. For ex...
View in text
Excerpt 4
) | 37 Table 16. .NET character representations (continued) Sequence Meaning \t Horizontal tab, \x09. \v Vertical tab, \x0B. \0octal Character specified by a...
View in text
Excerpt 5
. int preg_match_all (string pattern, string subject, array matches [, int flags]) Search for all matches of pattern against subject, and return the number o...
View in text
Excerpt 6
| 67 Table 35. Ruby comments and mode modifiers (continued) Mode character Meaning i Case-insensitive match based on characters in the current culture. n Tur...
View in text
Excerpt 7
$ End of search string, or the point before a string-ending newline, or before any newline if in multiline match mode, PCRE_MULTILINE. \Z End of string, or t...
View in text
Excerpt 8
string */ (int)strlen(text), /* length of input string */ 0, /* starting position in input string */ 0, /* OR'd options */ capturevector, /* holds results of...
View in text
Tags
AI categories
ProgrammingCodeTechnology
ISBN: 0596514271
Publisher: O'Reilly Media
Publish Year: 2007
Language: English
Pages: 128
File Format: PDF
File Size: 1002.3 KB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…