The Linux Command Line by Shotts William E
Author:Shotts, William E. [William E. Shotts Jr.]
Language: eng
Format: epub
Tags: COMPUTERS / Operating Systems / Linux
ISBN: 9781593274269
Publisher: No Starch Press
Published: 2012-01-14T16:00:00+00:00
Quantifiers
Extended regular expressions support several ways to specify the number of times an element is matched.
?—Match an Element Zero Times or One Time
This quantifier means, in effect, “Make the preceding element optional.” Let’s say we wanted to check a phone number for validity and we considered a phone number to be valid if it matched either of these two forms, (nnn) nnn-nnnn or nnn nnn-nnnn, where n is a numeral. We could construct a regular expression like this:
^\(?[0-9][0-9][0-9]\)? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$
In this expression, we follow the parentheses characters with question marks to indicate that they are to be matched zero or one time. Again, since the parentheses are normally metacharacters (in ERE), we precede them with backslashes to cause them to be treated as literals instead.
Let’s try it:
[me@linuxbox ˜]$ echo "(555) 123-4567" | grep -E '^\(?[0-9][0-9][0-9]\)? [0-9] [0-9][0-9]$' (555) 123-4567 [me@linuxbox ˜]$ echo "555 123-4567" | grep -E '^\(?[0-9][0-9][0-9]\)? [0-9] [0-9][0-9]-[0-9][0-9][0-9][0-9]$' 555 123-4567 [me@linuxbox ˜]$ echo "AAA 123-4567" | grep -E '^\(?[0-9][0-9][0-9]\)? [0-9] [0-9][0-9]-[0-9][0-9][0-9][0-9]$' [me@linuxbox ˜]$
Here we see that the expression matches both forms of the phone number but does not match one containing non-numeric characters.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Disaster & Recovery | Email Administration |
Linux & UNIX Administration | Storage & Retrieval |
Windows Administration |
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7781)
Grails in Action by Glen Smith Peter Ledbrook(7696)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6563)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6551)
Running Windows Containers on AWS by Marcio Morales(6081)
Kotlin in Action by Dmitry Jemerov(5065)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(4916)
Combating Crime on the Dark Web by Nearchos Nearchou(4498)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4414)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4335)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4170)
The Age of Surveillance Capitalism by Shoshana Zuboff(3950)
Python for Security and Networking - Third Edition by José Manuel Ortega(3738)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3508)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3407)
Mastering Python for Networking and Security by José Manuel Ortega(3344)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3330)
Blockchain Basics by Daniel Drescher(3294)
Learn Wireshark by Lisa Bock(3260)
