SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I match a bracket []?

    Can I search and replace by character codes or some other method to get these brackets out of my string? For example:

    $string = =~ s/[//;

    Will not work because perl sees the [ and thinks it is an unclosed statement. I have tried escaping with \[ but that gives me the same problem.
    recipe for food
    hypnosis hypnotic trance
    directory pages

  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)
    escaping the bracket is the correct way to go, the problem with your code is the improper use of the assignment operator "=" in the line of code you posted, should be:

    Code:
    $string = 'test [ test';
    $string =~ s/\[//; 
    print $string;
    In the future use the warnings pragma:

    Code:
    use warnings;
    Which will help you debug problems and potential problems in perl code.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Figures it was just a typo. Thanks!
    recipe for food
    hypnosis hypnotic trance
    directory pages

  4. #4
    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)
    You're welcome. Watch those typos, this was an easy one to spot but others are quite difficult. Using "strict" and "warnings" will help.


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
  •