Substrings using match


#1

Hey

So say suppose i have a string like this

Foo: 1, Bar: 2, Baz: 3

is it possible using match to get the value of Bar (2) ?

What i need is something that searches for substring Bar and then finds the appropriate value.

Something of the sort of (pseudocode)

match mystring {
   "Bar: @x" => x
}

#2

You could use regex for that. Or if your string is in some predefined format there’s probably a parser for it which might be even easier.


#3

Thanks. I’m torn between using regex or finding the substring manually and working it out.


#4

Use something like this:

\w+:\ ?(?<value>\d+)

Or even:

(?<name>\w+):\ ?(?<value>\d+)

So that you have the name stored.

This seems like the best solution for your use case.

EDIT: this seems like a good link for you rust-regex