Thread regex automatisch erstellen (6 answers)
Opened by kristian at 2007-10-20 19:09

kristian
 2007-10-20 22:28
#101139 #101139
User since
2005-04-14
684 Artikel
BenutzerIn
[Homepage] [default_avatar]
Code der bislang zu funktionieren scheint:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
sub _create_regex_block($$){
    my $first_number = shift;
    my $last_number  = shift;

    return $first_number if($first_number == $last_number);

    my ($string,@first_digits,@last_digits,$first_block,$last_block,$ctrl_number,@blocks,$this_block,$last_block_number);

    @first_digits = split(//,$first_number);
    @last_digits  = split(//,$last_number);

    if($first_digits[$#first_digits] != 0){
        $ctrl_number = join("", @first_digits[0..($#first_digits - 1)], 9);
        $ctrl_number += 0;
        if($ctrl_number > $last_number){
            $first_block = (($#first_digits)? join("", @first_digits[0..($#first_digits - 1)]) : '')  . '[' . $first_digits[$#first_digits] . '-' . $last_digits[$#last_digits] . ']';      
            return $first_block;
        }else{
            $first_block  = (($#first_digits)? join("", @first_digits[0..($#first_digits - 1)]) : '')  . '[' . $first_digits[$#first_digits] . '-9]';
            $first_number = (int($first_number / 10) * 10 ) + 10;
        }
    }

    if($last_digits[$#last_digits] != 9){
        $last_block  = (($#last_digits)? join("", @last_digits[0..($#last_digits - 1)]) : '') . '[0-' . $last_digits[$#last_digits] . ']';
        $last_number = (int($last_number / 10) * 10 ) - 1;      
    }
    if($last_number > $first_number){
        if(length($first_number) == 1){
            if($first_number == $last_number){
                $this_block = $first_number
            }else{
                $last_block_number = 9;
                $last_block_number = $last_number if($last_block_number > $last_number);
                $this_block = '[' .  $first_number . '-' . $last_block_number . ']';
                $first_number = 10 if($last_number >= 10);
            }
            push(@blocks, $this_block);
        }
        if(length($first_number) == 2){
            if($first_number == $last_number){
                $this_block = $first_number
            }else{
                @first_digits = split(//,$first_number);
                $last_block_number = 99;
                $last_block_number = $last_number if($last_block_number > $last_number);
                @last_digits  = split(//,$last_block_number);
                if($first_digits[0] == $last_digits[0]){
                    $this_block = $first_digits[0] . '[' . $first_digits[1]  . '-' . $last_digits[1] . ']';
                }else{
                    $this_block = '[' . $first_digits[0]  . '-' . $last_digits[0] . '][' . $first_digits[1]  . '-' . $last_digits[1] . ']';
                }
                $first_number = 100 if($last_number >= 100);
            }
            push(@blocks, $this_block);
        }
        if(length($first_number) == 3){
            if($first_number == $last_number){
                $this_block = $first_number
            }else{
                my $work_to_do = 1;
                while($work_to_do){
                    @first_digits = split(//,$first_number);
                    $last_block_number = (((int($first_number / 100) + 1) * 100) - 1); # 199 / 299
                    if($last_block_number >= $last_number){
                        $last_block_number = $last_number;
                        $work_to_do = 0;
                    }
                    @last_digits  = split(//,$last_block_number);
                    if($first_digits[1] == $last_digits[1]){
                        $this_block = $first_digits[0] . $first_digits[1] . '[' . $first_digits[2]  . '-' . $last_digits[2] . ']';
                    }else{
                        $this_block = $first_digits[0] . '[' . $first_digits[1]  . '-' . $last_digits[1] . '][' . $first_digits[2]  . '-' . $last_digits[2] . ']';
                    }
                    $first_number = ((int($last_block_number / 100) + 1) * 100);
                    $work_to_do = 0 if($first_number >= $last_number);
                    push(@blocks, $this_block);
                }
            }
        }
    }

    if(defined $first_block){
        unshift(@blocks, $first_block);
    }
    if(defined $last_block){
        push(@blocks, $last_block);
    }


    if(scalar(@blocks) > 1){
        $string = '(' . join('|', @blocks) . ')';
    }else{
        $string = $blocks[0];
    }

    return $string; 
}


EDIT:

Ich habe fertig.
Sieht nicht schön aus, scheint aber die Aufgabe zu lösen.
Ev. kann es ja mal eine(r) brauchen.

Gruss
Kristian

View full thread regex automatisch erstellen