class ICU::Regex

Overview

Regular Expressions

Unicode-aware regular expression matching and replacement.

Patterns follow the ICU Regular Expression syntax, which is largely compatible with Perl 5 regular expressions.

Usage

re = ICU::Regex.new("(\\w+)@(\\w+\\.\\w+)")
m = re.match("Contact: user@example.com")
m          # => "user@example.com"
m.group(1) # => "user"
m.group(2) # => "example.com"

re.scan("a1 b2 c3") { |m| puts m.group(0) }

ICU::Regex.new("\\d+").replace_all("foo 42 bar 7", "N") # => "foo N bar N"

See also

Defined in:

icu/regex.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(pattern : String, flags : Flag = Flag::None) #

Creates a new Regex from the given pattern string.

Raises ICU::Error if the pattern is invalid.

ICU::Regex.new("\\d+")
ICU::Regex.new("hello", ICU::Regex::Flag::CaseInsensitive)

[View source]

Instance Method Detail

def finalize #

[View source]
def flags : Flag #

Returns the flags used to create this regex.


[View source]
def match(text : String) : Match | Nil #

Attempts to match the pattern anywhere within the given string, returning a Match on success or nil if there is no match.

m = ICU::Regex.new("\\d+").match("foo 42")
m       # => "42"
m.range # => 4...6

[View source]
def matches?(text : String) : Bool #

Returns whether the pattern matches the given string in its entirety.

ICU::Regex.new("\\d+").matches?("42")   # => true
ICU::Regex.new("\\d+").matches?("42px") # => false

[View source]
def matches? : Bool #

Returns whether the entire input text (as set via #text=) matches the pattern.

re = ICU::Regex.new("\\d+")
re.text = "42"
re.matches? # => true
re.text = "abc"
re.matches? # => false

[View source]
def pattern : String #

Returns the pattern string used to create this regex.


[View source]
def replace_all(text : String, replacement : String) : String #

Replaces all non-overlapping matches of this pattern in text with replacement.

The replacement string may contain $0, $1, etc. to refer to matched groups.

ICU::Regex.new("\\d+").replace_all("foo 42 bar 7", "N") # => "foo N bar N"

[View source]
def replace_first(text : String, replacement : String) : String #

Replaces the first match of this pattern in text with replacement.

The replacement string may contain $0, $1, etc. to refer to matched groups.

ICU::Regex.new("\\d+").replace_first("foo 42 bar 7", "N") # => "foo N bar 7"

[View source]
def scan(text : String, &) : Nil #

Yields each non-overlapping match of the pattern in text as a Match.

ICU::Regex.new("\\d+").scan("a1 b22 c3") do |m|
  puts m.group(0)
end
# Output:
# 1
# 22
# 3

[View source]
def scan(text : String) : Array(String) #

Returns an array of all non-overlapping matches of the pattern in text.

ICU::Regex.new("\\d+").scan("a1 b22 c3") # => ["1", "22", "3"]

[View source]
def split(text : String) : Array(String) #

Splits text around matches of this pattern, returning the pieces.

ICU::Regex.new("\\s+").split("foo  bar baz") # => ["foo", "bar", "baz"]

[View source]
def text : String #

Returns the input text currently set on the regex.


[View source]
def text=(text : String) #

Sets the input text to search.

Resets the match position to the beginning of the text.


[View source]
def to_unsafe : LibICU::URegularExpression #

[View source]