SPVM::Regex - Regular Expression
use Regex; # Pattern match { my $re = Regex->new("ab*c"); my $string = "zabcz"; my $match = $re->match("zabcz"); } # Pattern match - UTF-8 { my $re = Regex->new("あ+"); my $string = "いあああい"; my $match = $re->match($string); } # Pattern match - Character class and the nagation { my $re = Regex->new("[A-Z]+[^A-Z]+"); my $string = "ABCzab"; my $match = $re->match($string); } # Pattern match with captures { my $re = Regex->new("^(\w+) (\w+) (\w+)$"); my $string = "abc1 abc2 abc3"; my $match = $re->match($string); if ($match) { my $cap1 = $re->cap1; my $cap2 = $re->cap2; my $cpa3 = $re->cap3; } } # Replace { my $re = Regex->new("abc"); my $string = "ppzabcz"; # "ppzABCz" my $result = $re->replace($string, "ABC"); my $replaced_count = $re->replaced_count; } # Replace with a callback and capture { my $re = Regex->new("a(bc)"); my $string = "ppzabcz"; # "ppzABbcCz" my $result = $re->replace($string, method : string ($re : Regex) { return "AB" . $re->cap1 . "C"; }); } # Replace global { my $re = Regex->new("abc"); my $string = "ppzabczabcz"; # "ppzABCzABCz" my $result = $re->replace_g($string, "ABC"); } # Replace global with a callback and capture { my $re = Regex->new("a(bc)"); my $string = "ppzabczabcz"; # "ppzABCbcPQRSzABCbcPQRSz" my $result = $re->replace_g($string, method : string ($re : Regex) { return "ABC" . $re->cap1 . "PQRS"; }); } # . - single line mode { my $re = Regex->new("(.+)", "s"); my $string = "abc\ndef"; my $match = $re->match($string); unless ($match) { return 0; } unless ($re->cap1 eq "abc\ndef") { return 0; } }
Regex provides regular expression.
Regex
Regex is a SPVM module.
The implementation is Google RE2.
SPVM is yet experimental status.
See Google RE2 Syntax.
has captures : ro string[];
Get the captured strings.
has match_start : ro int;
Get the start byte offset of the matched string.
has match_length : ro int;
Get the length of the matched string.
has replaced_count : ro int;
Get the replaced count.
static method new : Regex ($pattern : string, $flags = undef : string)
Create a new Regex object and compile the regex pattern and the flags.
my $re = Regex->new("^ab+c"); my $re = Regex->new("^ab+c", "s");
method match : int ($string : string, $offset = 0 : int, $length = -1 : int)
The Alias for the following match_forward method.
my $ret = $self->match_forward($string, \$offset, $length);
method match_forward : int ($string : string, $offset : int*, $length = -1 : int)
Execute pattern matching to the string range from the offset to the position proceeded by the length.
The offset is updated to the next starting position.
If the pattern matching is successful, return 1, otherwise return 0.
1
0
The string must be defined. Otherwise an exception will be thrown.
The offset + the length must be less than or equal to the length of the string. Otherwise an exception will be thrown.
The regex compililation is not yet performed. Otherwise an exception will be thrown.
method replace : string ($string : string, $replace : object of string|Regex::Replacer, $offset = 0 : int, $length = -1 : int, $options = undef : object[])
The Alias for the following replace_common method.
my $ret = $self->replace_common($string, $replace, \$offset, $length, $options);
method replace_g : string ($string : string, $replace : object of string|Regex::Replacer, $offset = 0 : int, $length = -1 : int, $options = undef : object[])
my $new_options_list = List->new($options); $new_options_list->push("global"); $new_options_list->push(1); $options = $new_options_list->to_array; return $self->replace_common($string, $replace, \$offset, $length, $options);
method replace_common : string ($string : string, $replace : object of string|Regex::Replacer, $offset_ref : int*, $length = -1 : int, $options = undef : object[])
Replace the part of the pattern matching in the string range from the offset to the position proceeded by the length with the replacement string or callback.
The options can be used.
The replacement must be a string or a Regex::Replacer object. Otherwise an exception will be thrown.
The offset must be greater than or equal to 0. Otherwise an exception will be thrown.
Internally match_forward is used for the pattern matching.
method split : string[] ($string : string, $limit = 0 : int);
The same as the split method in the Fn class, but the separator is a regular expression.
If global is a true value, the global replacement is performed.
global
method cap1 : string ()
The alias for $re->captures->[1].
$re->captures->[1]
method cap2 : string ()
The alias for $re->captures->[2].
$re->captures->[2]
method cap3 : string ()
The alias for $re->captures->[3].
$re->captures->[3]
method cap4 : string ()
The alias for $re->captures->[4].
$re->captures->[4]
method cap5 : string ()
The alias for $re->captures->[5].
$re->captures->[5]
method cap6 : string ()
The alias for $re->captures->[6].
$re->captures->[6]
method cap7 : string ()
The alias for $re->captures->[7].
$re->captures->[7]
method cap8 : string ()
The alias for $re->captures->[8].
$re->captures->[8]
method cap9 : string ()
The alias for $re->captures->[9].
$re->captures->[9]
method cap10 : string ()
The alias for $re->captures->[10].
$re->captures->[10]
method cap11 : string ()
The alias for $re->captures->[11].
$re->captures->[11]
method cap12 : string ()
The alias for $re->captures->[12].
$re->captures->[12]
method cap13 : string ()
The alias for $re->captures->[13].
$re->captures->[13]
method cap14 : string ()
The alias for $re->captures->[14].
$re->captures->[14]
method cap15 : string ()
The alias for $re->captures->[15].
$re->captures->[15]
method cap16 : string ()
The alias for $re->captures->[16].
$re->captures->[16]
method cap17 : string ()
The alias for $re->captures->[17].
$re->captures->[17]
method cap18 : string ()
The alias for $re->captures->[18].
$re->captures->[18]
method cap19 : string ()
The alias for $re->captures->[19].
$re->captures->[19]
method cap20 : string ()
The alias for $re->captures->[20].
$re->captures->[20]
SPVM::Regex - Github
Yuki Kimoto
Ryunosuke Murakami
Copyright Yuki Kimoto 2022-2022, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install SPVM::Regex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SPVM::Regex
CPAN shell
perl -MCPAN -e shell install SPVM::Regex
For more information on module installation, please visit the detailed CPAN module installation guide.