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

Folks, I need a regex to parse the output of a command. I would like to capture the domainname that comes after "name =". For instance, the whole string would look something like this. name = dominname.domain.dot.com. I need only "dominname.domain.dot.com" so that I can push that to an array and proceed further. I tried using this "/A-Za-z\.A-Za-z\.A-Za-z\.A-Za-z\./". It did not work. Any pointers? Thanks

Replies are listed 'Best First'.
Re: Need some help with Regex
by Corion (Patriarch) on Feb 07, 2010 at 09:33 UTC

    In what way did it not work? Did it not match the wanted lines? Did it match other lines? Did you want to capture the domain name?

    Please show a small self-contained example, together with representative input data and the output you want.

      Thank you for your prompt reply. Please go through the code below and let me know if I am missing something ..
      #!/usr/bin/perl $_ = `nslookup <some_ip_addr>`;/A-Za-z\.A-Za-z\.A-Za-z\.A-Za-z\./; print $&;
      The above snippet does not provide any output. I wanted a regex that would be more specific; that would capture the string right after "name = "

        Your A-Za-z should probably have been character classes, i.e. [A-Za-z] (or maybe simply \w, which also includes 0-9 and _ ).  Try

        #!/usr/bin/perl -l $_ = `nslookup 209.197.123.153`; print $1 if /name\s*=\s*(([A-Za-z]+\.){2,})/; __END__ www.perlmonks.org.

        The snipped does not provide any output because it's a basically senseless concatenation of valid Perl expressions. You need to read perlre to learn about regular expressions and maybe you want to read perlsyn to learn about Perls syntactic constructs like if.

        Also, you might want to provide a step by step description in prose of what your program is supposed to do - this might help you choose the correct Perl constructs.

        let me know if I am missing something

        Yup, you are missing the part in Corion's post that said: 'Please show a small self-contained example, together with representative input data and the output you want.'.

        You may find it helpful to read some of the regex docs: perlretut, perlre and perlreref.


        True laziness is hard work
Re: Need some help with Regex
by biohisham (Priest) on Feb 07, 2010 at 10:52 UTC
    The Regex you provided doesn't match a pattern because it is not passed a string to check whether that pattern exists...

    You haven't provided any sample input that makes us see how your data looks like hence there might not be much to guess, but since you only wanted to capture the string right after "name =" I have assumed this

    #!/usr/local/bin/perl use strict; use warnings; my $address= "name=domainname.domain.dot.com"; #the command output $address =~ s/name=//; print "$address\n"; my ($domainname, $domain)= $address=~ /(\w+)\.(\w+)\./; print "$domainname\n"; print "$domain\n";

    Investing time in reading the documentation subjects provided to you by GrandFather and Corion would be mighty rewarding


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Need some help with Regex
by planetscape (Chancellor) on Feb 07, 2010 at 18:44 UTC