matchall
matchall(r::Regex, s::AbstractString[, overlap::Bool=false]) -> Vector{AbstractString}
Return a vector of the matching substrings from eachmatch.
Examples
julia> matchall(r"[a-z]", "is a letter")
9-element Array{SubString{UTF8String},1}:
"i"
"s"
"a"
"l"
"e"
"t"
"t"
"e"
"r"
In the Julia programming language, the function matchall(r::Regex, s::AbstractString[, overlap::Bool=false])
is used to find all occurrences of a regular expression pattern r
in a given string s
. It returns a vector of the matching substrings from each match.
Here are some examples of how to use the matchall
function:
-
Find all occurrences of a pattern in a string:
julia> str = "Hello, Julia is awesome! Julia is fast."; julia> matches = matchall(r"Julia", str); julia> matches 2-element Array{SubString{String},1}: "Julia" "Julia"
This example finds all occurrences of the pattern "Julia" in the given string
str
and returns them as a vector of substrings. -
Retrieve all matching substrings in a case-insensitive manner:
julia> str = "Julia is a versatile language. julia is powerful."; julia> matches = matchall(r"julia"; ignorecase=true, overlap=false); julia> matches 2-element Array{SubString{String},1}: "Julia" "julia"
By setting the
ignorecase
parameter totrue
, the pattern matching becomes case-insensitive. Theoverlap
parameter is set tofalse
to avoid overlapping matches. - Handle overlapping matches:
julia> str = "ababab"; julia> matches = matchall(r"a"; overlap=true); julia> matches 3-element Array{SubString{String},1}: "a" "a" "a"
In this example, the
overlap
parameter is set totrue
to allow overlapping matches. Each occurrence of the pattern "a" is considered as a separate match.
Common mistake example:
julia> str = "Hello, Julia is awesome!";
julia> matches = matchall("Julia", str);
ERROR: MethodError: no method matching matchall(::String, ::String)
In this example, the regular expression pattern is provided as a string instead of a Regex
object. Make sure to use the Regex
constructor to create a regular expression object for pattern matching using matchall
.
See Also
eachmatch, ismatch, match, matchall,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.