Home: Perl Programming Help: Regular Expressions:
How to write the function this regular expression



kk2950
Novice

Feb 19, 2010, 1:29 AM


Views: 6799
How to write the function this regular expression

 
Hi,

I have a question related to the interview question. Trying to find a way to resolve.

Question:
string1 = "ab25AEBT312ab!!!"

Write the function to display only "312" using the regular expression and assume you did not know the string1 content. Write the function to show 3 numeric pattern.

The expect result will be
312

thanks,
kc


shawnhcorey
Enthusiast


Feb 19, 2010, 5:30 AM


Views: 6787
Re: [kk2950] How to write the function this regular expression

Well, if the question was written in English, it would be easier to understand. Do they mean: Write a function to extract sequences of 3 digits from a string?

Code
#!/usr/bin/perl 

use strict;
use warnings;

sub foo {
my @sequences = ();

for my $string ( @_ ){
my @seqs = $string =~ m{ [[:digit:]]{3} }gmsx;
push @sequences, @seqs;
}

return @sequences;
}

print foo( 'ab25AEBT312ab!!!' );


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


kk2950
Novice

Feb 19, 2010, 4:40 PM


Views: 6766
Re: [shawnhcorey] How to write the function this regular expression

Next time I should be clarify my question. This is very helpful and I learn something. Thank you