-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword-regex.raku
More file actions
executable file
·51 lines (43 loc) · 1.23 KB
/
word-regex.raku
File metadata and controls
executable file
·51 lines (43 loc) · 1.23 KB
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
#!/usr/bin/env raku
sub word-matches($w, :$string) is export {
my @matchesatches = $string.match(:global,
/ << $w >> /).map({
{ word => ~$_, start => $_.from, end => $_.to }
});
my $nm = @matchesatches.elems;
if $nm {
say "Found text '$w' $nm times:";
for @matchesatches.kv -> $i is copy, $w {
++$i;
say " $i. Position {$w<start>}";
}
}
else {
say "Word '$w' not found";
}
}
# sub word-matches($w, :$string --> List) is export {
my $string = " Foo : Bar ";
my $w = "Foo ";
word-matches $w, :$string;
# see last deepseek chat
# want: one match forward
# want: one matching from the end
# more subs from deepseek
sub find-all-text-chunks (
Str $haystack, # the string to search
Str $needle, # the text chunk of interest
--> List # list of hashes of match data
) is export {
my @matches;
my $pos = 0;
while $haystack.index($needle, $pos) -> $found-at {
@matches.push: {
chunk => $needle,
start => $found-at,
end => $found-at + $needle.chars - 1,
};
$pos = $found-at + 1; # start point for next search
}
@matches;
} # end of sub find-all-text-chunks