Regular Expressions with Julia

I had briefly used regular expressions, to check the format of a string, on a previous day. I got into a bit more for today's task, where there were a lot of inputs of the form:

light orange bags contain 1 dark maroon bag, 3 dim maroon bags, 5 striped green bags, 2 pale aqua bags.
wavy bronze bags contain 3 pale black bags, 5 bright turquoise bags, 4 pale orange bags.
dark coral bags contain 3 bright lavender bags, 1 pale bronze bag, 2 dull blue bags.
dim chartreuse bags contain 2 wavy plum bags, 4 wavy teal bags, 1 dark crimson bag.

I wanted to convert all this text into a dictionary, with the colours of the external bag as the key. I could get the keys just by splitting:

(colour, contents) = split(ln, " bags contain ")

Now I'm left for a string describing the contents of the bag. This can also be split to get a list of the contents:

split(contents, ',')

Each item in the list will look something like this:

1 dark maroon bag
3 dim maroon bags

I considered splitting the string, and then recombining the two words of the colour, and discarding the bag[s], to get the number and colour of each of the types of contents. But I thought I'd take the opportunity to experiment a bit more with Regular Expressions.

Julia allows different components of a Regular Expression to be given names, which can then be referenced later. So, in this case:

m = match(r"(?<count>\d+) (?<colour>\w+ \w+) bag", c)

Some bags have no other bags inside: in this cases, the Regular Expression is not matched, and m has a value of nothing. Otherwise, the named components can then be accessed as follows:

if m != nothing
    colour = m[:colour]
    count = parse(Int, m[:count])
end

social