Using Substr and Regular expressions.

Discussion in 'Perl Misc' started by kent.westmoreland@gmail.com, Feb 23, 2007.

  1. Guest

    Hello,

    I am attempting to take a string similar to the following as input:

    response_93_johndoe_1171994965031.xml

    and break it up into the following output:

    93johndoe.txt

    The format of the input will always be of the type
    "response_<somenumber>_<somename>_<longstringofnumbers>.xml"

    I would like this to be dynamic, such that I can run a perl script
    from the command line, and it will take the above file name as input,
    and give me the second name as output.

    Any Ideas?

    My Ultimate goal is to be able to create a log file based on the name
    of the input file and store it as a variable to be used later.

    Thanks,
    Kent
     
    , Feb 23, 2007
    #1
    1. Advertising

  2. Manish Guest

    On Feb 23, 9:44 am, ""
    <> wrote:
    > Hello,
    >
    > I am attempting to take a string similar to the following as input:
    >
    > response_93_johndoe_1171994965031.xml
    >
    > and break it up into the following output:
    >
    > 93johndoe.txt
    >
    > The format of the input will always be of the type
    > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
    >
    > I would like this to be dynamic, such that I can run a perl script
    > from the command line, and it will take the above file name as input,
    > and give me the second name as output.
    >
    > Any Ideas?
    >
    > My Ultimate goal is to be able to create a log file based on the name
    > of the input file and store it as a variable to be used later.
    >
    > Thanks,
    > Kent


    #!/usr/local/bin/perl -w

    use strict;
    my $pat = qr/response_(\d+)_(\w+)_.*.xml/;
    my $d = "response_93_johndoe_1171994965031.xml";
    my ($num, $name) = ($d =~ m"$pat");
    print "$num$name.txt\n";

    Should do the trick.
     
    Manish, Feb 23, 2007
    #2
    1. Advertising

  3. Dan Mercer Guest

    <> wrote in message news:...
    : Hello,
    :
    : I am attempting to take a string similar to the following as input:
    :
    : response_93_johndoe_1171994965031.xml
    :
    : and break it up into the following output:
    :
    : 93johndoe.txt
    :
    : The format of the input will always be of the type
    : "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
    :
    : I would like this to be dynamic, such that I can run a perl script
    : from the command line, and it will take the above file name as input,
    : and give me the second name as output.
    :
    : Any Ideas?
    :
    : My Ultimate goal is to be able to create a log file based on the name
    : of the input file and store it as a variable to be used later.

    My guess is that if you have to ask this question, you're not ready
    to program in perl yet. A number of solutions are available:

    use strict;
    use warnings;
    my $num;
    my $name;
    ($num,$name) = (split /_/,$ARGV[0])[1,2];

    is just one of many possibilities.

    Dan Mercer

    :
    : Thanks,
    : Kent
    :
     
    Dan Mercer, Feb 23, 2007
    #3
  4. Xicheng Jia Guest

    On Feb 23, 6:44 am, ""
    <> wrote:
    > Hello,
    >
    > I am attempting to take a string similar to the following as input:
    >
    > response_93_johndoe_1171994965031.xml
    >
    > and break it up into the following output:
    >
    > 93johndoe.txt
    >
    > The format of the input will always be of the type
    > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"
    >
    > I would like this to be dynamic, such that I can run a perl script
    > from the command line, and it will take the above file name as input,
    > and give me the second name as output.
    >
    > Any Ideas?
    >
    > My Ultimate goal is to be able to create a log file based on the name
    > of the input file and store it as a variable to be used later.
    >
    > Thanks,
    > Kent


    For your input, you can do it on the command line.

    echo response_93_johndoe_1171994965031.xml |
    perl -F_ -alne 'print "$F[1]$F[2].txt"'

    Regards,
    Xicheng
     
    Xicheng Jia, Feb 23, 2007
    #4
  5. Dr.Ruud Guest

    schreef:

    > I am attempting to take a string similar to the following as input:
    > response_93_johndoe_1171994965031.xml
    > and break it up into the following output:
    > 93johndoe.txt


    perl -wle'
    my $input = q{response_93_johndoe_1171994965031.xml};
    my $output = join(q{}, (split '_', $input)[1..2]) . q{.txt};
    print $output;
    '

    perl -wle'
    my $input = q{response_93_johndoe_1171994965031.xml};
    print qq{$1$2.txt} if $input =~ /_([0-9]+)_([a-z0-9]+)/;
    '

    (know that [a-z] can match more than 26 different characters)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Feb 23, 2007
    #5
  6. On Feb 23, 3:19 pm, "Manish" <> wrote:
    > On Feb 23, 9:44 am, ""
    >
    >
    >
    > <> wrote:
    > > Hello,

    >
    > > I am attempting to take a string similar to the following as input:

    >
    > > response_93_johndoe_1171994965031.xml

    >
    > > and break it up into the following output:

    >
    > > 93johndoe.txt

    >
    > > The format of the input will always be of the type
    > > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"

    >
    > > I would like this to be dynamic, such that I can run a perl script
    > > from the command line, and it will take the above file name as input,
    > > and give me the second name as output.

    >
    > > Any Ideas?

    >
    > > My Ultimate goal is to be able to create a log file based on the name
    > > of the input file and store it as a variable to be used later.

    >
    > > Thanks,
    > > Kent

    >
    > #!/usr/local/bin/perl -w
    >
    > use strict;


    Unless compatability with old Perl is an issue "use warnings" is
    preferable to the -w switch

    > my $pat = qr/response_(\d+)_(\w+)_.*.xml/;


    I think it would be better anchored.

    my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;

    > my ($num, $name) = ($d =~ m"$pat");


    Although the m operator _allows_ you to use non-standard regex
    delimiters, IMHO, it aids clairy not to do so for no reason:

    my ($num, $name) = $d =~ /$pat/;

    Actually you can say...

    my ($num, $name) = $d =~ $pat;

    ....but IMHO that's less clear.
     
    Brian McCauley, Feb 23, 2007
    #6
  7. Manish Guest

    On Feb 23, 12:54 pm, "Brian McCauley" <> wrote:
    > On Feb 23, 3:19 pm, "Manish" <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 23, 9:44 am, ""

    >
    > > <> wrote:
    > > > Hello,

    >
    > > > I am attempting to take a string similar to the following as input:

    >
    > > > response_93_johndoe_1171994965031.xml

    >
    > > > and break it up into the following output:

    >
    > > > 93johndoe.txt

    >
    > > > The format of the input will always be of the type
    > > > "response_<somenumber>_<somename>_<longstringofnumbers>.xml"

    >
    > > > I would like this to be dynamic, such that I can run a perl script
    > > > from the command line, and it will take the above file name as input,
    > > > and give me the second name as output.

    >
    > > > Any Ideas?

    >
    > > > My Ultimate goal is to be able to create a log file based on the name
    > > > of the input file and store it as a variable to be used later.

    >
    > > > Thanks,
    > > > Kent

    >
    > > #!/usr/local/bin/perl -w

    >
    > > use strict;

    >
    > Unless compatability with old Perl is an issue "use warnings" is
    > preferable to the -w switch
    >


    sure

    > > my $pat = qr/response_(\d+)_(\w+)_.*.xml/;

    >
    > I think it would be better anchored.
    >
    > my $pat = qr/^response_(\d+)_(\w+)_.*\.xml$/;
    >


    sure

    > > my ($num, $name) = ($d =~ m"$pat");

    >
    > Although the m operator _allows_ you to use non-standard regex
    > delimiters, IMHO, it aids clairy not to do so for no reason:
    >


    sorry, couldn't follow *use non-standard regex ...*, can you explain a
    little more?

    > my ($num, $name) = $d =~ /$pat/;
    >
    > Actually you can say...
    >
    > my ($num, $name) = $d =~ $pat;
    >
    > ...but IMHO that's less clear.- Hide quoted text -
    >
    > - Show quoted text -
     
    Manish, Feb 23, 2007
    #7
  8. "Manish" <> wrote in
    news::

    > On Feb 23, 12:54 pm, "Brian McCauley" <> wrote:
    >> On Feb 23, 3:19 pm, "Manish" <> wrote:


    ....

    >
    >> > my ($num, $name) = ($d =~ m"$pat");

    >>
    >> Although the m operator _allows_ you to use non-standard regex
    >> delimiters, IMHO, it aids clairy not to do so for no reason:
    >>

    >
    > sorry, couldn't follow *use non-standard regex ...*, can you explain a
    > little more?
    >
    >> my ($num, $name) = $d =~ /$pat/;


    Using the " characters as a delimeter in the match operator above is
    non-standard. While there is nothing preventing you from using that
    character, it is better not get too carried away and stick with the most
    common case unless there is a very good reason to do so. Hence, things
    are clearer if you use /$pat/ rather than m"$pat" which looks to me like
    "spat" and makes it harder to read your code.

    If what is between the delimeters contains a bunch of / characters, it
    is common to use braces of exclamation marks as delimiters.

    If you do not want variable interpolation, you should use single quotes
    as delimiters.

    Sinan
     
    A. Sinan Unur, Feb 23, 2007
    #8
  9. Guest

    On Feb 23, 10:21 am, "Dan Mercer" <> wrote:

    > My guess is that if you have to ask this question, you're not ready
    > to program in perl yet. A number of solutions are available:
    >
    > use strict;
    > use warnings;
    > my $num;
    > my $name;
    > ($num,$name) = (split /_/,$ARGV[0])[1,2];
    >
    > is just one of many possibilities.
    >
    > Dan Mercer
    >


    Dan,

    Your blunt response "My guess is that if you have to ask this
    question, you're not ready to program in perl yet." is not far from
    the truth.

    I only recently started to "learn" perl ( 2 days ago). I am modifying
    a script that was given to me to preform a specific task. I wanted to
    make the script easier to use but I was not having much luck with the
    tutorials I found on the web. I figured posting here was a great
    place to find information that I required.

    I have tried both Manish and your suggestions ( both of which work
    fine) with a few modifications.

    Some of my main holdbacks on perl where syntactical ( I actually had
    something similar to your version using the split function but only
    one variable).

    Anyway, thanks to everyone for all of your examples and help! Now I
    have a lot of things to research.
    Kent
     
    , Feb 23, 2007
    #9
  10. Dan Mercer Guest

    <> wrote in message news:...
    : On Feb 23, 10:21 am, "Dan Mercer" <> wrote:
    :
    : > My guess is that if you have to ask this question, you're not ready
    : > to program in perl yet. A number of solutions are available:
    : >
    : > use strict;
    : > use warnings;
    : > my $num;
    : > my $name;
    : > ($num,$name) = (split /_/,$ARGV[0])[1,2];
    : >
    : > is just one of many possibilities.
    : >
    : > Dan Mercer
    : >
    :
    : Dan,
    :
    : Your blunt response "My guess is that if you have to ask this
    : question, you're not ready to program in perl yet." is not far from
    : the truth.
    :
    : I only recently started to "learn" perl ( 2 days ago). I am modifying
    : a script that was given to me to preform a specific task. I wanted to
    : make the script easier to use but I was not having much luck with the
    : tutorials I found on the web. I figured posting here was a great
    : place to find information that I required.
    :
    : I have tried both Manish and your suggestions ( both of which work
    : fine) with a few modifications.
    :
    : Some of my main holdbacks on perl where syntactical ( I actually had
    : something similar to your version using the split function but only
    : one variable).
    :
    : Anyway, thanks to everyone for all of your examples and help! Now I
    : have a lot of things to research.
    : Kent

    Try SAMS "Teach Yourself Perl in 24 hours" (Clinton Pierce). Worked
    for me.

    Dan Mercer

    :
    :
    :
    :
     
    Dan Mercer, Feb 23, 2007
    #10
  11. Guest

    On Feb 23, 6:24 pm, "Dan Mercer" <> wrote:
    > <> wrote in messagenews:...
    >
    > : On Feb 23, 10:21 am, "Dan Mercer" <> wrote:
    > :
    > : > My guess is that if you have to ask this question, you're not ready
    > : > to program in perl yet. A number of solutions are available:
    > : >
    > : > use strict;
    > : > use warnings;
    > : > my $num;
    > : > my $name;
    > : > ($num,$name) = (split /_/,$ARGV[0])[1,2];
    > : >
    > : > is just one of many possibilities.
    > : >
    > : > Dan Mercer
    > : >
    > :
    > : Dan,
    > :
    > : Your blunt response "My guess is that if you have to ask this
    > : question, you're not ready to program in perl yet." is not far from
    > : the truth.
    > :
    > : I only recently started to "learn" perl ( 2 days ago). I am modifying
    > : a script that was given to me to preform a specific task. I wanted to
    > : make the script easier to use but I was not having much luck with the
    > : tutorials I found on the web. I figured posting here was a great
    > : place to find information that I required.
    > :
    > : I have tried both Manish and your suggestions ( both of which work
    > : fine) with a few modifications.
    > :
    > : Some of my main holdbacks on perl where syntactical ( I actually had
    > : something similar to your version using the split function but only
    > : one variable).
    > :
    > : Anyway, thanks to everyone for all of your examples and help! Now I
    > : have a lot of things to research.
    > : Kent
    >
    > Try SAMS "Teach Yourself Perl in 24 hours" (Clinton Pierce). Worked
    > for me.
    >
    > Dan Mercer
    >
    > :
    > :
    > :
    > :



    Hi Dan,

    Thanks for the recommendation! I will check that book out!

    Thanks for everyones help!
    Kent
     
    , Feb 26, 2007
    #11
  12. Broke Guest

    <> wrote:

    > > Try SAMS "Teach Yourself Perl in 24 hours" (Clinton Pierce). Worked
    > > for me.
    > >
    > > Dan Mercer
    > >
    > > :
    > > :
    > > :
    > > :

    For myself I would like to recommend the simply excellent
    book or Randal L. Schwartz, & Tom Christiansen
    Published by O'REILLY.

    The book is carefully paced and you have corrected exercises
    at the end of each chapter.
    This was tremendously useful for me because I am the learn
    by doing type of person.
    In a few minutes you will be able to write useful scripts !

    Many thanks to you Randal L. Swartz and the others I already
    mentioned: without you I would never had learned Perl. The
    book from Larry Wall was rather difficult for to digest because
    I am also a beginner. This is in fact this book of Larry Wall:
    programming Perl that you will need after reading the excellent
    book of Mr. Swartz and the others I already mentioned.

    I simply love Perl it became my obsession !
    --
    B.
     
    Broke, Mar 7, 2007
    #12
  13. Broke <> wrote:

    > For myself I would like to recommend the simply excellent
    > book or Randal L. Schwartz, & Tom Christiansen
    > Published by O'REILLY.



    That is the 2nd edition of the book, and kind of old.

    "Learning Perl" is currently in its 4th edition.


    > I simply love Perl it became my obsession !



    You are not alone.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 8, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    626
    Jay Douglas
    Aug 15, 2003
  2. Kees Hoogendijk

    getline and substr

    Kees Hoogendijk, Dec 20, 2003, in forum: C++
    Replies:
    4
    Views:
    566
    Kees Hoogendijk
    Dec 21, 2003
  3. Sean Nakasone
    Replies:
    10
    Views:
    177
    Dr.Ruud
    Oct 6, 2007
  4. arz
    Replies:
    9
    Views:
    161
    Uri Guttman
    Feb 5, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    245
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page