Substitute and replace

Discussion in 'Perl Misc' started by francan, Mar 2, 2010.

  1. francan

    francan Guest

    I have the below examples where I need to get the lastname using Perl
    substitute and replace:
    joeAAAAsmith jrAAAAbudget where I need to get smith jr
    or this
    GeneAAAAEvansAAAAmanager where I need to get Evans

    I need to fetch just the lastname which will always be in between the
    AAAA and AAAA delimeter: SteveAAAAJones-SingletonAAAAengineer where I
    would need to get Jones-Singleton

    I used the below and it worked but would like to know if this is the
    most efficient way to do it?


    s/(.*)AAAA(.*)AAAA(.*)/$2
     
    francan, Mar 2, 2010
    #1
    1. Advertisements

  2. Remember the old rule of thumb: if you know what to keep use s///, is
    you know what to throw away use split():

    $lastname = (split /AAAA/, $foo)[1];

    jue
     
    Jürgen Exner, Mar 2, 2010
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.