Professional Web Applications Themes

Regex question... - PERL Miscellaneous

In article <a2b8188a.0307140124.1343aeb6posting.google.com >, Math55 <magelordt-online.de> wrote: : hi, i have this regex: : : \.(?!.png|.log)[^.]*$ : : how can i replace the .before png and log with nothing? the problem : is, the alternation can be longer, like that: : : \.(?!.png|.log|.txt|.c|.cpp and so on )[^.]*$ Is the code below what you're after? #! /usr/local/bin/perl use warnings; use strict; my $extensions = qr/^(png|log)$/; while (<DATA>) { chomp; if (/^(.+)\.([^.]*)$/) { $_ = $1 unless $2 =~ $extensions; } print $_, "\n"; } __DATA__ foo.png bar.log baz.txt quux.c blurfl.cpp It produces this output: foo.png bar.log baz quux blurfl Sorry if ...

  1. #1

    Default Re: Regex question...

    In article <a2b8188a.0307140124.1343aeb6posting.google.com >,
    Math55 <magelordt-online.de> wrote:

    : hi, i have this regex:
    :
    : \.(?!.png|.log)[^.]*$
    :
    : how can i replace the .before png and log with nothing? the problem
    : is, the alternation can be longer, like that:
    :
    : \.(?!.png|.log|.txt|.c|.cpp and so on )[^.]*$

    Is the code below what you're after?

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    my $extensions = qr/^(png|log)$/;

    while (<DATA>) {
    chomp;

    if (/^(.+)\.([^.]*)$/) {
    $_ = $1 unless $2 =~ $extensions;
    }

    print $_, "\n";
    }

    __DATA__
    foo.png
    bar.log
    baz.txt
    quux.c
    blurfl.cpp

    It produces this output:

    foo.png
    bar.log
    baz
    quux
    blurfl

    Sorry if I misunderstood your question. Can you provide example inputs
    and the corresponding outputs you expect?

    Greg
    --
    The more corrupt the state, the more numerous the laws.
    -- Tacitus
    Greg Bacon Guest

  2. #2

    Default Re: Regex question...

    Math55 wrote:
    >hi, i have this regex:
    >
    >
    >\.(?!.png|.log)[^.]*$
    >
    >
    >how can i replace the .before png and log with nothing? the problem
    >is, the alternation can be longer, like that:
    >
    >\.(?!.png|.log|.txt|.c|.cpp and so on )[^.]*$
    I'm not sure on what you're asking, but this won't work, as you have two
    dots in a row, one in front of the lookahead, and one (though unescaped)
    inside it. And you don't have a anchor to the end of string.

    This, IMO, would look more like it:

    \.(?!(?:png|log|txt|c|cpp)$)[^.]*$


    --
    Bart.
    Bart Lateur Guest

Similar Threads

  1. regex question
    By Math55 in forum PERL Beginners
    Replies: 19
    Last Post: July 21st, 06:22 PM
  2. Maybe Stupid RegEx Question
    By Bastian Angerstein in forum PERL Beginners
    Replies: 2
    Last Post: February 13th, 03:14 PM
  3. Regex search question
    By Paul Harwood in forum PERL Beginners
    Replies: 3
    Last Post: November 7th, 04:52 AM
  4. A small regex question
    By Online in forum PHP Development
    Replies: 3
    Last Post: October 30th, 09:22 AM
  5. perl regex question
    By Chad Williams in forum PERL Miscellaneous
    Replies: 7
    Last Post: September 11th, 07:04 AM

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