Thread Bedingung in Regexp (4 answers)
Opened by roooot at 2009-04-25 15:11

murphy
 2009-04-25 17:08
#120884 #120884
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Ich würde es mal so versuchen:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use strict;
use warnings;

my @allowed = qw~
  foo.de
  bar.de
~;

my $allowed = join '|', map quotemeta($_), @allowed;

my $url_ok = qr~
  ^http://([^/]+\.)?($allowed)(/.*)?$
~ix;

while (my $url = <DATA>) {
  chomp $url;
  print "$url:\n";

  if ($url =~ m/$url_ok/) {
    print "  domain:    $2\n";
    print "  subdomain: $1\n";
    print "  path:      $3\n";
  }
  else {
    print "  not accepted\n";
  }

  print "\n";
}

__DATA__
http://www.example.com/foo/bar
http://www.foo.de/blubb
http://www.foo.de
http://bar.de/boing
http://bar.de
http://www.foo.de.vu/blubb
http://foo.de.vu/didumm
http://foo.de.vu
When C++ is your hammer, every problem looks like your thumb.

View full thread Bedingung in Regexp