Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to get substring between first and second character

by tale103108 (Novice)
on Jan 17, 2013 at 17:42 UTC ( #1013835=perlquestion: print w/ replies, xml ) Need Help??
tale103108 has asked for the wisdom of the Perl Monks concerning the following question:

Given strings like "Database_Test_Access_DB" and "Database_Development_Access_DB", I wish to get just "Test" and "Development" from the above-mentioned strings. In other words, I would like to get the string that starts after the first '_' and ends with the next (second) '_'. I would believe that a regex would be do the trick but would like your input. I respectfully ask for eternal guidance from those monks of the Perl monastery. Thank you.

Comment on How to get substring between first and second character
Re: How to get substring between first and second character
by choroba (Abbot) on Jan 17, 2013 at 17:49 UTC
    You can use split, which uses a regular expression to specify the separator. Only retrieve the second field:
    for my $string ("Database_Test_Access_DB", "Database_Development_Access_DB", ) { print((split /_/, $string)[1], "\n"); }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to get substring between first and second character
by BillKSmith (Chaplain) on Jan 17, 2013 at 18:38 UTC
    A regex will "do the trick".
    use strict; use warnings; my $DB_string = "Database_Test_Access_DB"; my ($get) = $DB_string =~ /_(.+?)_/; print $get, "\n";
    Bill

      Oh wise Bill... your solution worked. One more related question. I need to trim the last few characters which could be either CISD or ISD, so that 'TestISD' becomes 'Test' and 'DevelopmentCISD' becomes 'Development'. What wisdom can you impart upon this poor soul?

        s/C?ISD$// on the captured string, or modifying BillKSmith's regex (assuming those endings are in the the original strings) to /_(.+?)C?ISD_/ would work.

Re: How to get substring between first and second character
by pvaldes (Chaplain) on Jan 18, 2013 at 16:53 UTC

    Err..., a substring between two adjacent characters is always "".

    Probably, not the best title chosen for your problem.

    As said, a regex solves your problem easily:

    while (<DATA>){ if (/^.*?\_(.*?)(CI|I)SD\_.*?$/) {print $1,"\n"} } __DATA__ my_firstmachISD_blah_blah the_secondCISD_blah_blah_blah

    Updated: I need to trim the last few characters which could be either CISD or ISD

    Updated again: yes, C?ISD is better than (CI|I)SD

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013835]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-11-27 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls