class ICU::Regex
- ICU::Regex
- Reference
- Object
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.crConstructors
-
.new(pattern : String, flags : Flag = Flag::None)
Creates a new Regex from the given pattern string.
Instance Method Summary
- #finalize
-
#flags : Flag
Returns the flags used to create this regex.
-
#match(text : String) : Match | Nil
Attempts to match the pattern anywhere within the given string, returning a
Matchon success ornilif there is no match. -
#matches?(text : String) : Bool
Returns whether the pattern matches the given string in its entirety.
-
#matches? : Bool
Returns whether the entire input text (as set via
#text=) matches the pattern. -
#pattern : String
Returns the pattern string used to create this regex.
-
#replace_all(text : String, replacement : String) : String
Replaces all non-overlapping matches of this pattern in text with replacement.
-
#replace_first(text : String, replacement : String) : String
Replaces the first match of this pattern in text with replacement.
-
#scan(text : String, &) : Nil
Yields each non-overlapping match of the pattern in text as a
Match. -
#scan(text : String) : Array(String)
Returns an array of all non-overlapping matches of the pattern in text.
-
#split(text : String) : Array(String)
Splits text around matches of this pattern, returning the pieces.
-
#text : String
Returns the input text currently set on the regex.
-
#text=(text : String)
Sets the input text to search.
- #to_unsafe : LibICU::URegularExpression
Constructor Detail
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)
Instance Method Detail
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
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
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
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"
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"
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
Returns an array of all non-overlapping matches of the pattern in text.
ICU::Regex.new("\\d+").scan("a1 b22 c3") # => ["1", "22", "3"]
Splits text around matches of this pattern, returning the pieces.
ICU::Regex.new("\\s+").split("foo bar baz") # => ["foo", "bar", "baz"]
Sets the input text to search.
Resets the match position to the beginning of the text.