Amar Daadaa
Amar Daadaa

Reputation: 73

Using a regex in Perl, how do I find the first occurrence of any letter?

I am stumped; I've tried a few things so far but what I am trying to extract is the first letter in a Perl string.

I have for example:

10emV

I want to use a regex to extract the first letter, which in this case would be e.

Upvotes: 3

Views: 933

Answers (3)

abra
abra

Reputation: 575

my $let = $1 if '10emV' =~ m/([a-z]+?)/g;
print $let;

Upvotes: -1

DavidO
DavidO

Reputation: 13942

if ( $string =~ m/([[:alpha:]])/ ) {
    print $1, $/;
}

Upvotes: 0

Kobi
Kobi

Reputation: 138027

You can simply search for \p{L}, or [a-zA-z] for ASCII letters. The first match is the first letter.

If you want to match the start of the string (for some reason), you can also use \A\P{L}*\p{L}, or \A[^a-zA-z]*[a-zA-z].

See also: Perl regular expressions tutorial - More on characters, strings, and character classes

Upvotes: 6

Related Questions