CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



kk2950
Novice

Feb 19, 2010, 1:29 AM

Post #1 of 3 (2806 views)
How to write the function this regular expression Can't Post

 
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

Post #2 of 3 (2794 views)
Re: [kk2950] How to write the function this regular expression [In reply to] Can't Post

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

Post #3 of 3 (2773 views)
Re: [shawnhcorey] How to write the function this regular expression [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives