Name

SPVM::Regex - Regular Expression

Usage

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_cb($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_cb($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;
  }
}

Description

Regex provides regular expression.

Regex is a SPVM module.

The implementation is Google RE2.

Caution

SPVM is yet experimental status.

Regular Expression Syntax

See Google RE2 Syntax.

Fields

captures

has captures : ro string[];

Get the captured strings.

match_start

has match_start : ro int;

Get the start byte offset of the matched string.

match_length

has match_length : ro int;

Get the length of the matched string.

replaced_count

has replaced_count : ro int;

Get the replaced count.

Class Methods

new

static method new : Regex ($pattern_and_flags : string[]...)

Create a new Regex object and compile the regex pattern with the flags.

my $re = Regex->new("^ab+c");
my $re = Regex->new("^ab+c", "s");

Instance Methods

match

method match : int ($string : string)

The Alias for the following match_offset method.

my $offset = 0;
$re->match_offset($string, \$offset);

match_offset

method match_offset : int ($string : string, $offset_ref : int*)

Execute pattern matching to the string and the starting offset of the string.

The offset is updated to the next starting position.

If the pattern matching is successful, return 1, otherwise return 0.

replace

method replace  : string ($string : string, $replace : string)

The Alias for the following replace_offset method.

my $offset = 0;
$re->replace_offset($string, \$offset, $replace);

replace_cb

method replace_cb  : string ($string : string, $replace_cb : Regex::Replacer)

The Alias for the following replace_cb_offset method.

my $offset = 0;
$re->replace_cb_offset($string, \$offset, $replace_cb);

replace_g

method replace_g  : string ($string : string, $replace : string)

The Alias for the following replace_g_offset method.

my $offset = 0;
$re->replace_g_offset($string, \$offset, $replace);

replace_g_cb

method replace_g_cb  : string ($string : string, $replace_cb : Regex::Replacer)

The Alias for the following replace_g_cb_offset method.

my $offset = 0;
$re->replace_g_cb_offset($string, \$offset, $replace_cb);

replace_offset

method replace_offset  : string ($string : string, $offset_ref : int*, $replace : string)

Replace the part of the pattern matching in the string with the replacement string from the starting offset of the string.

The offset is updated to the next starting position.

replace_cb_offset

method replace_cb_offset  : string ($string : string, $offset_ref : int*, $replace_cb : Regex::Replacer)

Replace the part of the pattern matching with the replacement callback that is Regex::Replacer object from the starting offset of the string.

The offset is updated to the next starting position.

replace_g_offset

method replace_g_offset  : string ($string : string, $offset_ref : int*, $replace : string)

Replace all of the part of the pattern matching with the replacement string from the starting offset of the string.

The offset is updated to the next starting position.

replace_g_cb_offset

method replace_g_cb_offset  : string ($string : string, $offset_ref : int*, $replace_cb : Regex::Replacer)

Replace all of the part of the pattern matching with the replacement callback that is Regex::Replacer object from the starting offset of the string.

The offset is updated to the next starting position.

cap1

method cap1 : string ()

The alias for $re->captures->[1].

cap2

method cap2 : string ()

The alias for $re->captures->[2].

cap3

method cap3 : string ()

The alias for $re->captures->[3].

cap4

method cap4 : string ()

The alias for $re->captures->[4].

cap5

method cap5 : string ()

The alias for $re->captures->[5].

cap6

method cap6 : string ()

The alias for $re->captures->[6].

cap7

method cap7 : string ()

The alias for $re->captures->[7].

cap8

method cap8 : string ()

The alias for $re->captures->[8].

cap9

method cap9 : string ()

The alias for $re->captures->[9].

cap10

method cap10 : string ()

The alias for $re->captures->[10].

cap11

method cap11 : string ()

The alias for $re->captures->[11].

cap12

method cap12 : string ()

The alias for $re->captures->[12].

cap13

method cap13 : string ()

The alias for $re->captures->[13].

cap14

method cap14 : string ()

The alias for $re->captures->[14].

cap15

method cap15 : string ()

The alias for $re->captures->[15].

cap16

method cap16 : string ()

The alias for $re->captures->[16].

cap17

method cap17 : string ()

The alias for $re->captures->[17].

cap18

method cap18 : string ()

The alias for $re->captures->[18].

cap19

method cap19 : string ()

The alias for $re->captures->[19].

cap20

method cap20 : string ()

The alias for $re->captures->[20].