line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Util::Regex; |
2
|
|
|
|
|
|
|
|
3
|
10
|
|
|
10
|
|
75
|
use Catmandu::Sane; |
|
10
|
|
|
|
|
36
|
|
|
10
|
|
|
|
|
78
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
10
|
|
|
10
|
|
86
|
use Exporter qw(import); |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
3153
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
10
|
|
|
|
|
|
|
escape_regex |
11
|
|
|
|
|
|
|
as_regex |
12
|
|
|
|
|
|
|
substituter |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK,); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _escape_regex { |
18
|
24
|
|
|
24
|
|
69
|
my ($str) = @_; |
19
|
24
|
|
|
|
|
98
|
$str =~ s/\//\\\//g; |
20
|
24
|
|
|
|
|
50
|
$str =~ s/\\$/\\\\/; # pattern can't end with an escape |
21
|
24
|
|
|
|
|
55
|
$str; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub as_regex { |
25
|
21
|
|
|
21
|
1
|
48
|
my ($str) = @_; |
26
|
21
|
|
|
|
|
63
|
$str = _escape_regex($str); |
27
|
21
|
|
|
|
|
294
|
qr/$str/; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub substituter { |
31
|
3
|
|
|
3
|
1
|
1253
|
my ($search, $replace) = @_; |
32
|
3
|
|
|
|
|
8
|
$search = as_regex($search); |
33
|
3
|
|
|
|
|
9
|
$replace = _escape_regex($replace); |
34
|
|
|
|
|
|
|
eval |
35
|
3
|
|
|
|
|
396
|
qq|sub {my \$str = \$_[0]; utf8::upgrade(\$str); \$str =~ s/$search/$replace/g; \$str}|; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
1; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
__END__ |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=pod |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 NAME |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Catmandu::Util::Regex - Regex related utility functions |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 FUNCTIONS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=over 4 |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item as_regex($str) |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Escapes and quotes the given string as a regex. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item substituter($search, $replace) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Builds a function that performs a regex substitution. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $ltrimmer = substituter('^[\h\v]+', ''); |
61
|
|
|
|
|
|
|
$ltrimmer->(" eek! "); |
62
|
|
|
|
|
|
|
# => "eek! " |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=back |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |