Find the answer to your Linux question:
Results 1 to 2 of 2
I'm working on a regular expression using the Perl engine. I need a regex that matches a domain, when the pattern precedes the Top Level Domain. I thought I had ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    10

    Match pattern in specific part of domain

    I'm working on a regular expression using the Perl engine. I need a regex that matches a domain, when the pattern precedes the Top Level Domain. I thought I had the problem fixed up just fine with this regex:

    Code:
    ^[^/]*images
    That regex matches imagesstock.com & coolimages.net, etc, but doesn't match dell.com/foimages/laptop.html, etc. That's working mighty fine.

    However, the regex shown above DOES match subdomains images.tigerdirect.com, and I hadn't intended those subdomains to match.

    What is the correct syntax to match:
    imagesstock.com
    coolimages.net

    But skip
    dell.com/foimages/laptop.html AND
    images.tigerdirect.com

    I had tried
    Code:
    ^[^/]*images[^\.*\.]
    and
    ^[^/]*images[^(\.*\.)]
    but the [] are giving alteration behavior instead of grouping. I think that regex would work if I could just find the syntax to group \.*\. into one pattern.

    Thanks a million for any ideas you might have.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm assuming you also want to accept as valid:

    imagesstock.com/asdf.html

    Try this Perl script, adjusting the top line to match the path to perl on your system:
    Code:
    #!/usr/bin/perl
    
    while($in_line=<STDIN>)
    {
      chomp($in_line);
    
      if($in_line=~/^[^\/]*images[^\/\.]*\.[a-z0-9]{2,4}(\/.*)?$/)
      {
        print "yes\n";
      }
      else
      {
        print "no\n";
      }
    }
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...