matchall(r::Regex, s::AbstractString[, overlap::Bool=false]) -> Vector{AbstractString}

Return a vector of the matching substrings from eachmatch.


julia> matchall(r"[a-z]", "is a letter")
9-element Array{SubString{UTF8String},1}:

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:

  1. 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}:

    This example finds all occurrences of the pattern "Julia" in the given string str and returns them as a vector of substrings.

  2. 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}:

    By setting the ignorecase parameter to true, the pattern matching becomes case-insensitive. The overlap parameter is set to false to avoid overlapping matches.

  3. Handle overlapping matches:
    julia> str = "ababab";
    julia> matches = matchall(r"a"; overlap=true);
    julia> matches
    3-element Array{SubString{String},1}:

    In this example, the overlap parameter is set to true 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.

*Required Field

Checking you are not a robot: