Regular Expressions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Regular Expressions

Description:

Some examples of special characters are period, brackets, asterisk, caret, and dollar sign. ... Caret ^ A regular expression that begins with a caret can match ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 13
Provided by: Kami47
Category:

less

Transcript and Presenter's Notes

Title: Regular Expressions


1
Regular Expressions
  • What is it?
  • Simple string
  • Special Characters

2
What is it?
  • It is a method of defining a set of one or more
    strings of characters.
  • Linux utilities such as vi, grep, awk, sed, use
    regular expression to search for and replace
    strings.
  • A regular expression matches any string it
    defines.

3
Simple String
  • A simple string matches only itself.
  • Example
    1. regular expression /ring/ matches
    ring, spring, ringing, stringing
    2. /or not/ matches or
    not, poor nothing 3. /Thursday/ matches
    Thursday, Thursdays

4
Special Characters
  • We can use special characters within a regular
    expression to cause the regular expression to
    match more than one string.
  • Some examples of special characters are period,
    brackets, asterisk, caret, and dollar sign.

5
Period (.)
  • A period matches any character.
  • Example
    1. / .alk/ matches all
    strings consisting of a SPACE followed by any
    character followd by alk such as will talk, may
    balk
  • 2. /.ing/ matches all strings consisting of
    any characters preceding ing such as singing,
    ping

6
Square Brackets
  • defines a character class that matches any
    sing character within the brackets.
  • Examples
    /bBill/ gt bill, Bill, billed
    /taeiou.k/gttalkative, stink, teak, tanker
  • Note! If the first character following the left
    square bracket is a caret (), it matches any
    single character not within the brackets.

    /a-zA-Z/ matches any
    character that is not a letter gt 1, 7, _at_, .,
    , !

7
Asterisk
  • An asterisk represents zero or more occurrences
    of the preceding regular expression.

    1. /abc/ a followed by zero or more bs
    followed by a c gt ac, abc, abbc, dcabbc
  • 2. /a-zA-Z / matches a string composed only
    of letters and spaces gt any string without
    numbers or punctuation.

8
Period and Asterisk (.)
  • An asterisk following a period matches any string
    of characters.
    1. /ab.c/ matches ab followed
    by zero or more characters followed by c. gtabc,
    abxc, ab45c, xab 756.345 x cat
  • 2. /t.ing/ macthes t followed by zero or more
    characters followed by ing gtthing, ting, I
    thought of going

    3. /(.)/ matches as long a string as
    possible between ( and ). gt (this), (that)

9
Caret
  • A regular expression that begins with a caret can
    match a string only at the beginning of a line.
  • 1. /T/ matches a T at the beginning of a
    line gt This line , That Time,
    2. /0-9/ matches a
    plus sign followed by a digit at the beginning of
    a line gt 5 79879, 759 keep this .

10
Dollar Sing
  • A dollar sign at the end of a regular expression
    matches the end of a line.

  • // matches
    a colon that ends a line gt .below

11
Quoting Special Characters
  • We can quote any special character (but not a
    digit or a parenthesis) by preceding it with a
    backslash.
  • Remember! Quoting a special character makes it
    represent itself.
  • 1. /end\./ gt The end., send.,
    pretend.mail
    2. / \\
    / gt A single back slash \
    3. / \/ gt An asterisk

12
Task
  • What does the regular expression of /Th.is/
    matches when applied to the following text
  • This (rug) is not what it once was ( a long time
    ago), is it?
Write a Comment
User Comments (0)
About PowerShow.com