Understanding Regular Expressions, Special Characters, and Patterns

Similar documents
Command-Line Interface

Password Management Guidelines for Cisco UCS Passwords

Advanced Handle Definition

Catalyst 6500 Series Switch SSL Services Module Command Reference

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Catalyst 6500 Series Switch SSL Services Module Command Reference

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

SPEECH RECOGNITION COMMON COMMANDS

Using the Command-Line Interface (CLI)

Chapter 17. Fundamental Concepts Expressed in JavaScript

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Regular Expressions & Automata

Catalyst 6500 Series Switch MSFC Command Reference. Cisco IOS Release 12.1(13)E

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Regular Expressions. Regular expressions match input within a line Regular expressions are very different than shell meta-characters.

Paolo Santinelli Sistemi e Reti. Regular expressions. Regular expressions aim to facilitate the solution of text manipulation problems

Pattern Matching. An Introduction to File Globs and Regular Expressions

Computer Systems and Architecture

Pattern Matching. An Introduction to File Globs and Regular Expressions. Adapted from Practical Unix and Programming Hunter College

TI-84+ GC 3: Order of Operations, Additional Parentheses, Roots and Absolute Value

Regular Expressions. Regular Expression Syntax in Python. Achtung!

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CMSC 132: Object-Oriented Programming II

Using Basic Formulas 4

Using Lex or Flex. Prof. James L. Frankel Harvard University

Computer Systems and Architecture

JavaScript Functions, Objects and Array

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Sensitive Data Detection

chapter 2 G ETTING I NFORMATION FROM A TABLE

Regular Expressions Explained

Structure of Programming Languages Lecture 3

LING115 Lecture Note Session #7: Regular Expressions

Expr Language Reference

ITST Searching, Extracting & Archiving Data

Fundamentals of Programming Session 4

IT 374 C# and Applications/ IT695 C# Data Structures

Using Microsoft Excel

Regular Expression Reference

How to Use Adhoc Parameters in Actuate Reports

CSE 401 Midterm Exam 11/5/10

Full file at

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Dr. Sarah Abraham University of Texas at Austin Computer Science Department. Regular Expressions. Elements of Graphics CS324e Spring 2017

CdmCL Language - Syntax

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Perl Programming. Bioinformatics Perl Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3


Pseudocode is an abbreviated version of the actual statement t t (or code ) in the program.

This page covers the very basics of understanding, creating and using regular expressions ('regexes') in Perl.

sed Stream Editor Checks for address match, one line at a time, and performs instruction if address matched

Regular Expressions 1

Effective Programming Practices for Economists. 17. Regular Expressions

Regular Expressions. Upsorn Praphamontripong. CS 1111 Introduction to Programming Spring [Ref:

Programming for Engineers Introduction to C

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Test Bank for An Object Oriented Approach to Programming Logic and Design 4th Edition by Joyce Farrell

Formulas in Microsoft Excel

Essentials for Scientific Computing: Stream editing with sed and awk

successes without magic London,

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

PESIT Bangalore South Campus

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

CSC 467 Lecture 3: Regular Expressions

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Syntax errors are produced when verifying an EasyLanguage statement that is not

Regular Expressions. Michael Wrzaczek Dept of Biosciences, Plant Biology Viikki Plant Science Centre (ViPS) University of Helsinki, Finland

Data Types and Variables in C language

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

How Actuate Reports Process Adhoc Parameter Values and Expressions

Lexical Considerations

This book is licensed under a Creative Commons Attribution 3.0 License

1 Lexical Considerations

Regular Expressions. Perl PCRE POSIX.NET Python Java

Appendix C. Numeric and Character Entity Reference

NOTES: String Functions (module 12)

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Language Fundamentals

S206E Lecture 19, 5/24/2016, Python an overview

Formulas and Functions

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Lexical Considerations

Regex, Sed, Awk. Arindam Fadikar. December 12, 2017

1 CS580W-01 Quiz 1 Solution

More Examples. Lex/Flex/JLex

Chapter 2. Lexical Elements & Operators

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Transcription:

APPENDIXA Understanding Regular Expressions, Special Characters, and Patterns This appendix describes the regular expressions, special or wildcard characters, and patterns that can be used with filters to search through command output. The filter commands are described in the Filtering show Command Output. Contents Regular Expressions, page A-151 Special Characters, page A-152 Character Pattern Ranges, page A-152 Multiple-Character Patterns, page A-153 Complex Regular Expressions Using Multipliers, page A-153 Pattern Alternation, page A-154 Anchor Characters, page A-154 Underscore Wildcard, page A-154 Parentheses Used for Pattern Recall, page A-155 Regular Expressions A regular expression is a pattern (a phrase, number, or more complex pattern). Regular expressions are case sensitive and allow for complex matching requirements. Simple regular expressions include entries like Serial, misses, or 138. Complex regular expressions include entries like 00210..., ( is ), or [Oo]utput. A regular expression can be a single-character pattern or multiple-character pattern. It can be a single character that matches the same single character in the command output or multiple characters that match the same multiple characters in the command output. The pattern in the command output is referred to as a string. The simplest regular expression is a single character that matches the same single character in the command output. Letters (A to Z and a to z), digits (0 to 9), and other keyboard characters (such as! or ~) can be used as a single-character pattern. A-151

Special Characters Appendix A Special Characters Certain keyboard characters have special meaning when used in regular expressions. Table A-1 lists the keyboard characters that have special meaning. Table A-1 Characters with Special Meaning Character Special Meaning. Matches any single character, including white space. * Matches 0 or more sequences of the pattern. + Matches 1 or more sequences of the pattern.? Matches 0 or 1 occurrences of the pattern. ^ Matches the beginning of the string. $ Matches the end of the string. _ (underscore) Matches a comma (,), left brace ({), right brace (}), the beginning of the string, the end of the string, or a space. To use these special characters as single-character patterns, remove the special meaning by preceding each character with a double backslash (\\). In the following examples, single-character patterns matching a dollar sign, an underscore, and a plus sign, respectively, are shown. \\$ \\_ \\+ Character Pattern Ranges A range of single-character patterns can be used to match command output. To specify a range of single-character patterns, enclose the single-character patterns in square brackets ([ ]). Only one of these characters must exist in the string for pattern-matching to succeed. For example, [aeiou] matches any one of the five vowels of the lowercase alphabet, while [abcdabcd] matches any one of the first four letters of the lowercase or uppercase alphabet. You can simplify a range of characters by entering only the endpoints of the range separated by a dash ( ), as in the following example: [a da D] To add a dash as a single-character pattern in the search range, precede it with a double backslash: [a da D\\ ] A bracket (]) can also be included as a single-character pattern in the range: [a da D\\ \\]] Invert the matching of the range by including a caret (^) at the start of the range. The following example matches any letter except the ones listed: [^a dqsv] The following example matches anything except a right square bracket (]) or the letter d: [^\\]d] A-152

Appendix A Multiple-Character Patterns Multiple-Character Patterns Multiple-character regular expressions can be formed by joining letters, digits, and keyboard characters that do not have a special meaning. With multiple-character patterns, order is important. The regular expression a4% matches the character a followed by a 4 followed by a %. If the string does not have a4%, in that order, pattern matching fails. The multiple-character regular expression a. uses the special meaning of the period character to match the letter a followed by any single character. With this example, the strings ab, a!, and a2 are all valid matches for the regular expression. Put a backslash before the keyboard characters that have special meaning to indicate that the character should be interpreted literally. Remove the special meaning of the period character by putting a backslash in front of it. For example, when the expression a\\. is used in the command syntax, only the string a. is matched. A multiple-character regular expression containing all letters, all digits, all keyboard characters, or a combination of letters, digits, and other keyboard characters is a valid regular expression. For example: telebit 3107 v32bis. Complex Regular Expressions Using Multipliers Multipliers can be used to create more complex regular expressions that instruct Cisco IOS XR software to match multiple occurrences of a specified regular expression. Table A-2 lists the special characters that specify multiples of a regular expression. Table A-2 Special Characters Used as Multipliers Character Description * Matches 0 or more single-character or multiple-character patterns. + Matches 1 or more single-character or multiple-character patterns.? Matches 0 or 1 occurrences of a single-character or multiple-character pattern. The following example matches any number of occurrences of the letter a, including none: a* The following pattern requires that at least one occurrence of the letter a in the string be matched: a+ The following pattern matches the string bb or bab: ba?b The following string matches any number of asterisks (*): \\** To use multipliers with multiple-character patterns, enclose the pattern in parentheses. In the following example, the pattern matches any number of the multiple-character string ab: (ab)* As a more complex example, the following pattern matches one or more instances of alphanumeric pairs: ([A-Za-z][0-9])+ A-153

Pattern Alternation Appendix A The order for matches using multipliers (*, +, and?) is to put the longest construct first. Nested constructs are matched from outside to inside. Concatenated constructs are matched beginning at the left side of the construct. Thus, the regular expression matches A9b3, but not 9Ab3 because the letters are specified before the numbers. Pattern Alternation Alternation can be used to specify alternative patterns to match against a string. Separate the alternative patterns with a vertical bar ( ). Only one of the alternatives can match the string. For example, the regular expression codex telebit matches the string codex or the string telebit, but not both codex and telebit. Anchor Characters Anchoring can be used to match a regular expression pattern against the beginning or end of the string. Table A-3 shows that regular expressions can be anchored to a portion of the string using the special characters. Table A-3 Special Characters Used for Anchoring Character Description ^ Matches the beginning of the string. $ Matches the end of the string. For example, the regular expression ^con matches any string that starts with con, and sole$ matches any string that ends with sole. In addition to indicating the beginning of a string, the ^ can be used to indicate the logical function not when used in a bracketed range. For example, the expression [^abcd] indicates a range that matches any single letter, as long as it is not the letters a, b, c, and d. Underscore Wildcard Use the underscore to match the beginning of a string (^), the end of a string ($), space ( ), braces ({}), comma (,), and underscore (_). With the underscore character, you can specify that a pattern exists anywhere in the input string. For example, _1300_ matches any string that has 1300 somewhere in the string and is preceded by or followed by a space, brace, comma, or underscore. Although _1300_ matches the regular expression {1300_, it does not match the regular expressions 21300 and 13000. The underscore can replace long regular expression lists. For example, instead of specifying ^1300$ {1300,,1300, {1300}, simply specify _1300_. A-154

Appendix A Parentheses Used for Pattern Recall Parentheses Used for Pattern Recall Use parentheses with multiple-character regular expressions to multiply the occurrence of a pattern. The Cisco IOS XR software can remember a pattern for use elsewhere in the regular expression. To create a regular expression that recalls a previous pattern, use parentheses to indicate memory of a specific pattern and a double backslash (\\) followed by a digit to reuse the remembered pattern. The digit specifies the occurrence of a parenthesis in the regular expression pattern. When there is more than one remembered pattern in the regular expression, \\1 indicates the first remembered pattern, \\2 indicates the second remembered pattern, and so on. The following regular expression uses parentheses for recall: a(.)bc(.)\\1\\2 This regular expression matches an a followed by any character (call it character number 1), followed by bc followed by any character (character number 2), followed by character number 1 again, followed by character number 2 again. So, the regular expression can match azbctzt. The software remembers that character number 1 is Z and character number 2 is T, and then uses Z and T again later in the regular expression. A-155

Parentheses Used for Pattern Recall Appendix A A-156