How can I remove all characters from string starting at first non-alpha character?

11,701

Solution 1

$s =~ s/[^a-zA-Z].*$//;

Literally, find the first non-alpha char and chop everything off starting from it.

Solution 2

You phrased the request 2 ways:

  1. Get all the alpha chars off the front of these strings
  2. Find the last alpha char and chop everything off after

While the result is the same given your sample strings, I've found it pays to be more careful with regexes. So, I'd take the first item above as the real requirement, and write it as:

$str =~ s/^([a-z]*)[^a-z].*/$1/i;

The advantage in my mind is that unexpected strings (like "7KENP989SD") should result in a null string after substitution, instead of something unexpected like "7KENP". Of course, maybe that is what you wanted...

Solution 3

s/([A-Za-z]*).*/$1/

... will work. It's not necessarily the best way of doing it, but it's a general case replace.

It only works if you just want alpha characters

Solution 4

s/\P{Alpha}.*// works for me fine:

perl -pe 's/\P{Alpha}.*//' <<EOF
KENP989SD
KENP913E
KENPX189R
KENP913
EOF

Solution 5

Here's my go at it.

/^([A-Za-z]).$/


EDIT I like Igor's approach better than mine ..


code:

#!/usr/bin/perl
#
# http://stackoverflow.com/questions/507941/perl-regex-remove-all-characters-from-string-after-last-alpha-character
#
use strict;
use warnings;
for my $string (<DATA>){
    $string =~ /^([A-Za-z]*).*$/;
    print "$1\n";
}
__DATA__
KENP989SD
KENP913E
KENPX189R
KENP913
Share:
11,701
CheeseConQueso
Author by

CheeseConQueso

facebook.com/CheeseConQueso - Facebook pocketband.net - uLoops/PocketBand grooveshark.com/CheeseConQueso - Grooveshark

Updated on June 06, 2022

Comments

  • CheeseConQueso
    CheeseConQueso almost 2 years

    This would have been a lot easier if not for certain situations.

    Sample data:

    KENP989SD
    KENP913E
    KENPX189R
    KENP913
    

    What regular expression can I use to remove all characters from the string starting at the first non-alpha character? Basically, I want to find the first non-alpha character and chop everything off after that regardless of char type.

    After regex is applied, these data should be:

    KENP
    KENP
    KENPX
    KENP