Why is matchall not defined?

I’m learning regexes and matchall is mentioned in a number of references, but when I try it, Julia says it’s not defined. match() returns too much garbola when I just want the matched string. Here’s an example right from the book and it doesn’t work:

julia> matchall(r"[a-z]", "is a letter")
ERROR: UndefVarError: matchall not defined
Stacktrace:

Did you check also how many years those references where published? Julia 1.0 was released in August 2018, any “reference” older than that should be taken with a pinch of salt.

Today you probably want to use eachmatch instead:

julia> eachmatch(r"[a-z]", "is a letter")
Base.RegexMatchIterator(r"[a-z]", "is a letter", false)

julia> collect(eachmatch(r"[a-z]", "is a letter"))
9-element Vector{RegexMatch}:
 RegexMatch("i")
 RegexMatch("s")
 RegexMatch("a")
 RegexMatch("l")
 RegexMatch("e")
 RegexMatch("t")
 RegexMatch("t")
 RegexMatch("e")
 RegexMatch("r")
2 Likes

Thanks. I see it’s been discontinued. The docs are current but much of them are written on a comp sci grad level so I often check elsewhere, and three books and four web refs had matchall. We need a set of simpleton docs :grinning:

However, eachmatch returns a qualifier in addition to the string I want. i.e.
RegexMatch(“602-921-4444”)
RegexMatch(“6029214444”)

I don’t want the RegexMatch(), I just want the strings. Is there a way to do that? Oddly, matchall did return just the strings, so its replacement is worse, IMHO.

1 Like
help?> RegexMatch
search: RegexMatch

  RegexMatch


  A type representing a single match to a Regex found in a string. Typically created from the match function.

  The match field stores the substring of the entire matched string. The captures field stores the substrings for each capture
  group, indexed by number. To index by capture group name, the entire match object should be indexed instead, as shown in the
  examples. The location of the start of the match is stored in the offset field. The offsets field stores the locations of the
  start of each capture group, with 0 denoting a group that was not captured.

  This type can be used as an iterator over the capture groups of the Regex, yielding the substrings captured in each group.
  Because of this, the captures of a match can be destructured. If a group was not captured, nothing will be yielded instead of a
  substring.

  Methods that accept a RegexMatch object are defined for iterate, length, eltype, keys, haskey, and getindex, where keys are the
  the names or numbers of a capture group. See keys for more information.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> m = match(r"(?<hour>\d+):(?<minute>\d+)(am|pm)?", "11:30 in the morning")
  RegexMatch("11:30", hour="11", minute="30", 3=nothing)
  
  julia> hr, min, ampm = m;
  
  julia> hr
  "11"
  
  julia> m["minute"]
  "30"
  
  julia> m.match
  "11:30"

tldr: based on how many matched results there are, you can use only - if you know there can be one and only one result.

1 Like