SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl: Regex Quick question

    If you try and substitute "is" for "was" in the string "This is silly" using
    Code Perl:
     s/is/was/ig
    you won't get "This was silly", you'll get "Thwas was silly".



    how would i correct this so it only subs is on its own with was.
    so then the substituted value is "This was silly"?

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the \b anchor:

    Code:
    $_ = 'This is silly';
    s/\bis\b/was/ig;
    print;
    You can look it up yourself in the perl documentation or online


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •