Home > Back-end >  Julia - Extract number from string using regex
Julia - Extract number from string using regex

Time:11-17

I have a list of strings each telling me after how many iterations an algorithm converged.

string_list = [
    "Converged after 1 iteration", 
    "Converged after 20 iterations", 
    "Converged after 7 iterations"
]

How can I extract the number of iterations? The result woudl be [1, 20, 7]. I tried with regex. Apparently (?<=after )(.*)(?= iteration*) will give me anything in between after and iteration but then this doesn't work:

occursin(string_list[1], r"(?<=after )(.*)(?= iteration*)")

CodePudding user response:

There's a great little Julia package that makes creating regexes easier called ReadableRegex, and as luck would have it the first example in the readme is an example of finding every integer in a string:

julia> using ReadableRegex

julia> reg = @compile look_for(
                       maybe(char_in(" -")) * one_or_more(DIGIT),
                       not_after = ".",
                       not_before = NON_SEPARATOR)
r"(?:(?<!\.)(?:(?:[ \-])?(?:\d) ))(?!\P{Z})"

That regex can now be broadcast over your list of strings:

julia> collect.(eachmatch.(reg, string_list))
3-element Vector{Vector{RegexMatch}}:
 [RegexMatch("1")]
 [RegexMatch("20")]
 [RegexMatch("7")]
  • Related