Physics_Student
Physics_Student

Reputation: 698

Julia - Extract number from string using regex

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*)")

Upvotes: 2

Views: 1421

Answers (2)

Sundar R
Sundar R

Reputation: 14695

To extract information out of a regex, you want to use match and captures:

julia> convergeregex = r"Converged after (\d+) iteration"
r"Converged after (\d+) iteration"

julia> match(convergeregex, string_list[2]).captures[1]
"20"

julia> parse.(Int, [match(convergeregex, s).captures[1] for s in string_list])
3-element Vector{Int64}:
  1
 20
  7

\d+ matches a series of digits (so, the number of iterations here), and the parantheses around it indicates that you want the part of the string matched by that to be placed in the results captures array.

You don't need the lookbehind and lookahead operators (?<=, ?=) here.

Upvotes: 3

Nils Gudat
Nils Gudat

Reputation: 13800

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")]

Upvotes: 3

Related Questions