1

I am trying to split a file into 2 parts.

File format is following: filename_status_date.filetype

and the files are:

apple_india_20110218091255.txt
apple_india_20110221112444.txt
apple_india_20110301112444.txt

I need to split so that first part carries "apple_india"
and second part carries the "20110301112444.txt".

I tried the following:

my ($fruit,$end)=split(/\_/);

But that splits the file right after the first "_" as

$fruit = apple
$end = india

How can I make it work. My arguments in Split() is wrong.
Any help

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by boshu
0
#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>){
    my ($fruit,$end)=split(/\_ #Match an underscore
                           (?!.*\_) #Negative lookahead.
                                    #Match only if not followed by characters
                                    #followed by another underscore
                           /x);     #x means allow multi-line regex with comments
    print "My fruit = $fruit\n";
    print "My end = $end\n";
}
__DATA__
apple_india_20110218091255.txt
apple_india_20110221112444.txt
apple_india_20110301112444.txt

http://www.regular-expressions.info/lookaround.html

0

Perfect solution.
And thanks for wonderful explanation David!

Learned a few new things here...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.