in reply to Re: Need some help with Regex
in thread Need some help with Regex

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 = "

Replies are listed 'Best First'.
Re^3: Need some help with Regex
by almut (Canon) on Feb 07, 2010 at 11:10 UTC

    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.
      I've recently started using Perl. All apologies for not being able to express the problem lucidly. almut, seems to have got what I was looking for...
      /name\s*=\s*([A-Za-z].+\w)/
      I used  \w as per your advise and got what I was really looking for... This works like a charm!! Thanks again, for sending me perlre document (I've bookmarked it :-) ) Cheers!!

        That may not be doing what you expect! Consider:

        #!/usr/bin/perl use strict; use warnings; for my $tail ('Z.z', '1.z', 'a(x)z', 'aaa', 'aa_', 'xxx@') { my $str = "name = $tail"; if ($str =~ /name\s*=\s*([A-Za-z].+\w)/) { print "Matched >$1< in >$str<\n"; } else { print "No match for >$str<\n"; } }

        Prints:

        Matched >Z.z< in >name = Z.z< No match for >name = 1.z< Matched >a(x)z< in >name = a(x)z< Matched >aaa< in >name = aaa< Matched >aa_< in >name = aa_< Matched >xxx< in >name = xxx@<

        [A-Za-z] matches a single letter. .+ matches at least one characters and as many as it possibly can such that the remainder of the regular expression matches. \w matches a single alphanumeric or underscore character.


        True laziness is hard work
Re^3: Need some help with Regex
by Corion (Patriarch) on Feb 07, 2010 at 10:50 UTC

    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.

Re^3: Need some help with Regex
by GrandFather (Saint) on Feb 07, 2010 at 10:36 UTC
    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