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:
need to extract string before non word char

 



dillidamunda
Novice

Nov 2, 2009, 10:47 AM

Post #1 of 3 (3586 views)
need to extract string before non word char Can't Post

my $message_temp = 'Perl @Programming';

#Match anything before "($1)non word character" or "($2)underscore" and "($3) any word character except underscore"
$message_temp =~/(.*(\W|_))([a-zA-Z0-9]*)$/;
#returns
print("$1"); # prints: Perl @
print("$2"); # prints: @
print("$3"); # prints: Programming

I want to fetch $1 as "Perl" not "Perl @", what change should i make in my regex? Thanks in advance.


shawnhcorey
Enthusiast


Nov 2, 2009, 1:02 PM

Post #2 of 3 (3582 views)
Re: [dillidamunda] need to extract string before non word char [In reply to] Can't Post

Use (?: ... ) for non-capturing parenthesis. See:

perldoc perlretut http://perldoc.perl.org/perlretut.html
perldoc perlre http://perldoc.perl.org/perlre.html

__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".


dillidamunda
Novice

Nov 13, 2009, 8:15 AM

Post #3 of 3 (3227 views)
Re: [shawnhcorey] need to extract string before non word char [In reply to] Can't Post

Thanks a ton for your help!

 
 


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

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