shroh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts, I am looking for some steps to include in my perl script code to do the following: 1) I have name=servername.xyz.abc.coom, I want to exclude the first part of this string i.e servername in a variable. How can i extract from the above string so that i can use it somewhere in my code. 2) I have name=servername , i want to still capture the first part in a variable. 3) I have name=servername.xyz.com, i still want this in a variable. So basically i need to capture just the first part in a variable and use it. Any help is highly appreciated.

Replies are listed 'Best First'.
Re: Steps to get a substring from a string
by CountZero (Bishop) on Jul 21, 2015 at 16:48 UTC
    Just to make sure I understand you fully.

    You have the following four patterns of data:

    1. name=servername.xyz.abc.coom
    2. name=servername
    3. name=servername.xyz.com
    and for each of these patterns you want to extract servername.

    If the above assumptions are correct, the following will do the trick:

    use Modern::Perl qw/2015/; while (<DATA>) { chomp; say "$_ => $1" if m/name=([^\.]+)/; } __DATA__ name=servername.xyz.abc.coom name=servername name=servername.xyz.com
    Output:
    name=servername.xyz.abc.coom => servername name=servername => servername name=servername.xyz.com => servername
    update:

    To be fully compliant with the respective RFCs (see above), the regex should be m/name=([-0-9a-z]+)/i

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      if m/name=([^\.]+)/;

      Small remark: there's no need to escape '.' (dot) in a character class.

        I know. It is just a (bad?) habit I have and to be a bit more orthogonal between the use is "." inside and outside of character classes.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
      Hi Let me reiterate what i told earlier, We have three scenarios: 1) Variable a can be servername.abc.xyz.com 2) It can be servername.xyz.com 3) It can be just servername I all the above cases i am just intereested in servername and if it can be saved in a variable.
        Let me reiterate ...

        Unfortunately, you're not reiterating anything: none of the example strings you give is the same as any in the OP. I suggest you follow CountZero's approach and post something like

        I have the following patterns of data:
        • 'name=servername.xyz.abc.coom'
        • 'name=servername'
        • 'name=servername.xyz.com'
        • 'whatever'
        • ...
        From each of these patterns I want to extract string.
        I think I know what you think you want, but why should we play guessing games? The devil is in the details.

        Update: Also, what version(s) of Perl must you work with? This has implications for the regex constructs that are available.


        Give a man a fish:  <%-(-(-(-<

        Wait! You have just changed the data. Before it was of the general form "name=servername ..." and now it is "servername ...".

        Different forms of data will need different forms of regexes.

        Please provide some better information or our help will just be guesswork.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
        Let me reiterate what CountZero told you earlier
        if ($string =~ m/name=([^.]+)/) { my $variable_i_am_interested_in = $1; }
Re: Steps to get a substring from a string
by Corion (Patriarch) on Jul 21, 2015 at 16:35 UTC

    Depending on the parts you have already written and the functionality you need, Mozilla::PublicSuffix might help you to determine what is "a domain" and what is "a hostname in a domain".

    If your question is only about string manipulation, have you considered using split on a dot and taking the first result?

    my @parts = split /\./, $input;
      Hi Corion

      How about the 2nd case,2) I have name=servername , i want to still capture the first part in a variable. In this your code will not work. we need to match a-zA-Z character after =

      my $str='name=host.com' my ($match) =$string=~/=([a-zA-Z]+)/;
      Update:

      Updated the regex part as per CountZero's node Re^3: Steps to get a substring from a string


      All is well. I learn by answering your questions...
        According to RFC952 and RFC-1123, the hostname should only consists of the ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', and the hyphen ('-'). Spaces and underscores are not allowed!

        Update: added "and underscores"

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Steps to get a substring from a string
by vinoth.ree (Monsignor) on Jul 21, 2015 at 16:32 UTC

    What did you try so far? show us the code you have tried.


    All is well. I learn by answering your questions...