Schrift
[thread]9202[/thread]

$rofl =~ /hallo{2,}/i



<< >> 5 Einträge, 1 Seite
Gast Gast
 2007-07-18 15:49
#78597 #78597
hallo kann mir bitte mal einer sagen warum das nicht geht?

$rofl = "hallo und so! hallo";

if ($rofl =~ /hallo{2,}/i) {
print "hallo";
}

sobald er 2x oder mehr als 2x hallo findet soll er was ausgeben ;)
Taulmarill
 2007-07-18 15:58
#78598 #78598
User since
2004-02-19
1750 Artikel
BenutzerIn

user image
Dein Regex würde voraussetzen, [s]dass die "hallo"s direkt hintereinander stehen[/s]. So würde ich das lösen:

Code: (dl )
if ( $rofl =~ /(?:hallo.*){2}/ ) {


edit: Da du das hallo in deinem Regex nicht gruppiert hast, bezieht sich das {2,} natürlich nicht auf das ganze Wort "hallo", sondern nur auf das "o".\n\n

<!--EDIT|Taulmarill|1184760084-->
$_=unpack"B*",~pack"H*",$_ and y&1|0& |#&&print"$_\n"for@.=qw BFA2F7C39139F45F78
0A28104594444504400 0A2F107D54447DE7800 0A2110453444450500 73CF1045138445F4800 0
F3EF2044E3D17DE 8A08A0451412411 F3CF207DF41C79E 820A20451412414 83E93C4513D17D2B
styx-cc
 2007-07-18 16:04
#78599 #78599
User since
2006-05-20
533 Artikel
BenutzerIn

user image
So ginge das, aber geht bestimmt noch 10x schoener ;)
Code: (dl )
$rofl =~ /.*(hallo).*(hallo).*/i


Du prüfst nur ob 2x das o hintereinander vorkommen.

MfG

edit: da wer wer schneller und huebscher :P\n\n

<!--EDIT|styx-cc|1184760313-->
Pörl.
shigetsu
 2007-07-20 04:54
#78600 #78600
User since
2007-04-22
16 Artikel
BenutzerIn
[Homepage] [default_avatar]
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
#!/usr/bin/perl -l

use strict;
use warnings;

use Data::Dumper qw(Dumper);

{
    my $boundary = 2;
    my $keyword = 'hallo';

    my %table = map { $_ => join ' ', ($keyword, 'foo') x $_ } (0, 1, 2, 3, 4);

    my $does_match = sub {
        my ($str) = @_;

        my $occurences = 0;
        $occurences++ while $str =~ /(?:\A|\b)$keyword(?:\b|\z)/g;

        return { occurences => $occurences, match => ($occurences >= $boundary) ? 'yes' : 'no' };
    };

    print Dumper \%table;

    foreach my $often (sort { $a <=> $b } keys %table) {
        my $retval = $does_match->($table{$often});
        print "occurences: $retval->{occurences}, match: $retval->{match}";
    }
}

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
$VAR1 = {
'4' => 'hallo foo hallo foo hallo foo hallo foo',
'1' => 'hallo foo',
'3' => 'hallo foo hallo foo hallo foo',
'0' => '',
'2' => 'hallo foo hallo foo'
};

occurences: 0, match: no
occurences: 1, match: no
occurences: 2, match: yes
occurences: 3, match: yes
occurences: 4, match: yes
jubei
 2007-07-20 13:35
#78601 #78601
User since
2007-07-19
22 Artikel
BenutzerIn
[default_avatar]
Quote
Code (perl): (dl )
(?:\A|\b)$keyword(?:\b|\z)

\b sollte eigentlich auch am Stringanfang/-ende matchen
<< >> 5 Einträge, 1 Seite



View all threads created 2007-07-18 15:49.