Professional Web Applications Themes

Stupid perl regexp question - PERL Miscellaneous

This is a really stupid regexp question but can someone please explain to me why this doesn't work: if( m/\/{1}/ ) { print $_; } I want to match only those lines which contain 1 and only 1 "/" character. My understanding is that a forward slash needs escaped or else it would signal the end of the regexp, the {1} indicates to match one and only one time. I have seen examples such as: $string =~ m/^\S{1,8}\.\S{0,3}/; which clearly show the syntax of both escaping and repetition and I can't see for the life of me how mine is ...

  1. #1

    Default Stupid perl regexp question

    This is a really stupid regexp question but can someone please explain
    to me why this doesn't work:

    if( m/\/{1}/ ) { print $_; }

    I want to match only those lines which contain 1 and only 1 "/"
    character. My understanding is that a forward slash needs escaped or
    else it would signal the end of the regexp, the {1} indicates to match
    one and only one time. I have seen examples such as:

    $string =~ m/^\S{1,8}\.\S{0,3}/;

    which clearly show the syntax of both escaping and repetition and I
    can't see for the life of me how mine is different, except that I
    don't care about the pattern matching at the beginning of the string.

    Output is returning lines that have 1 or more "/" chars. TIA for any
    help. BTW, please do not reply with suggestions to use grep, or
    sed/awk, or python, or C, or Lisp, or Smalltalk or any other languages
    or techniques. I want to use Perl for what I am doing.

    G. Klinedinst
    G Guest

  2. #2

    Default Re: Stupid perl regexp question

    >>>>> "GK" == G Klinedinst <com> writes:

    GK> help. BTW, please do not reply with suggestions to use grep, or
    GK> sed/awk, or python, or C, or Lisp, or Smalltalk or any other
    GK> languages or techniques. I want to use Perl for what I am doing.

    i doubt anyone here would suggest anything but perl for what you are
    trying to do.

    uri

    --
    Uri Guttman ------ com -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guest

  3. #3

    Default Re: Stupid perl regexp question

    G Klinedinst wrote:

    (snipped)
     
     


    if ($_ =~ tr¡/¡¡ == 1)
    { print "Found One Boss."; }


    Purl Gurl
    --
    Perl FAQs, Perl Doentation
    Apache FAQs, Apache Doentation
    http://www.purlgurl.net
    Purl Guest

  4. #4

    Default Re: Stupid perl regexp question

    you could try... (note, not tested)

    if (/\/(.*)/) {
    print $_ unless ($1 =~ /\//);
    }

    i'm sure someone here could show you something neater though :)

    Dagmar


    "G Klinedinst" <com> wrote in message
    news:google.com... 


    D Guest

  5. #5

    Default Re: Stupid perl regexp question

    G Klinedinst <com> wrote: 
     

    It works, it's just not what you want to happen... :-)

    Since you haven't anchored anything, that will match any string which
    has one slash in it. Not "one and only one", just "one".
     
     

    In general the regex /x{1,5}/ is equivalent to /x/, because if there are
    5, there's at least 1, and /x/ will match.

    They are different only when
    1) you are caputuring data or
    2) you are specifying data on both sides of the expression

    In your example, the \S{1,8} *must* be bounded on the front by the
    beginning of the string, and on the rear by a dot (\.). If the front of
    string specification weren't there, the {1,8} expression would be no
    more useful than a single \S, because 1, 8, or a thousand characters in
    the string would still allow a match.

    Another way to say "one and only one slash", is "starts with any number
    of non-slash characters, has a slash, and ends with any number of
    non-slash characters.
     

    You can use different characters to delmit the regex. When you want to
    match forward slashes, that can come in handy.
     

    Lets try constructing a regex for my restatement of your target string,
    using an alternate delimiter, and a character class. [^/] will
    represent all characters other than a forward slash.

    m!^[^/]*/[^/]*$!

    You could probably do the same thing with negative lookahead and
    lookbehind, but I'm less familiar with those techniques.

    --
    Darren Dunham com
    Unix System Administrator Taos - The SysAdmin Company
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Guest

  6. #6

    Default Re: Stupid perl regexp question

    [posted & mailed]

    On 22 Sep 2003, G Klinedinst wrote:
     

    That regex doesn't say "does $_ have ONLY one / in it?", it says "does $_
    have a / in it?". To get what you want, you'll need to use either

    print if tr!/!! == 1;

    which uses the tr/// operator (which returns the count of the characters
    it matches in your string, so "alphabet" =~ tr/ab// would return 3, for
    the a, a, and b it finds), or else

    print if m{^[^/]*/[^/]*$};

    which matches a string containing zero or more non-slashes, followed by a
    slash, followed by zero or more non-slashes, and anchored at the beginning
    and end of the string.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

    Jeff Guest

  7. #7

    Default Re: Stupid perl regexp question

    Thank you all for your help. I have learned a number of different ways
    to do this but more importantly I think I understand why is wasn't
    working in the first place.
    It sounds like the {n} operator means to look for n number of
    consecutive "/" strings which can occur any number of times in the
    string, instead of what I had imagined it meant.
    Changing the delimeter helps a bunch, thanks to all who made that
    suggestion. Also that tr/// trick where it returns the number of
    characters translated is great!


    G. Klinedinst
    G Guest

  8. #8

    Default Re: Stupid perl regexp question

    [This followup was posted to comp.lang.perl.misc]

    In article <google.com>,
    com says... 

    Your regexp will match any string that contains 1 consecutive "/" at any
    point in the string. Therefor if you have 2 "/" at different points in
    the string your pattern will still produce a match.

    And if i have a choice when it comes to text processing and pattern
    matching I will always choose Perl.
    Barry Guest

Similar Threads

  1. Stupid Question
    By Al Millstein in forum PERL Beginners
    Replies: 24
    Last Post: October 14th, 09:58 PM
  2. Stupid question.
    By duit in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: April 4th, 07:04 PM
  3. some stupid questions about string search & replace in perl
    By walala in forum PERL Miscellaneous
    Replies: 21
    Last Post: September 23rd, 06:29 AM
  4. a stupid question
    By Joe in forum PHP Development
    Replies: 2
    Last Post: July 24th, 10:37 AM
  5. simple, stupid perl package Q
    By Joseph Cipale in forum PERL Miscellaneous
    Replies: 2
    Last Post: July 15th, 04:10 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139