in reply to regex to remove all non a-z and spaces

$string =~ s/[^a-z8-9 ]+//gi
If 8-9 was a typo in OP, then change it to 0-9.

Replies are listed 'Best First'.
Re^2: regex to remove all non a-z and spaces
by mrborisguy (Hermit) on May 16, 2005 at 02:52 UTC
    a question on implementation... would the + make the regex any faster, since it matches more before it substitues?
    not that it matters at all in a case like this... just wondering, to file away in my "pieces of worthless trivia" section of the brain.
      I was thinking the same thing as i posted that, and I don't know -- I was actually thinking of posting it as a question.. i guess i'll do that now.

      Update: I forked this to a new thread: regex internals: quantifiers vs global match

        Synopsis from forked thread:  Yes it does.  At least for average and special cases (pathological ones are another matter).

Re^2: regex to remove all non a-z and spaces
by coldfingertips (Pilgrim) on May 16, 2005 at 02:52 UTC
    I'm not good with regexes to any degree but doesn't ^ just mean to match at the beginning of the string? How is this removing everything but a-z and numbers?
      usually, ^ does mean at the beginning, but the [ and ] make a character class, and a ^ at the beginning of a character class means "not any of these".

      Update:
      Oddly enough, it doesn't explicitly say that in perlre. however, it does say
      You can negate the [::] character classes by prefixing the class name with a '^'. This is a Perl extension.
        So what it's really saying is "s/// anything that is not within our class"? So s/[^0-9\.-]//g; would be an example on how to remove any non number (except - and .)?

        Oops, that's a strange omission in the docs (though negating character classes is briefly mentioned in perlrequick and perlretut.

        Just for the record, it is specifically the negation of POSIX character classes that is a perl extension - /[^a-z]/ is straight from Henry Spencer's original code.

        Hugo