I have a string “Page 1 of 15”.
I need to get the value 15 as this value could be any number between 1 and 100. I’m trying to figure out:
-
If regular expressions are best suited here. Considering string will never change maybe just split the string by spaces? Or a better solution.
-
How to get the value using a regular expression.
Regular expression you can use:
Page \d+ of (\d+)Analysis of expression: between
(and)you capture a group. The\dstands for digit, the+for one or more digits. Note that it is important to be exact, so copy the spaces as well.The code is a tad verbose, but I figured it’d be better understandable this way. With a split you just need:
var pages = input.Split(' ')[3];, looks easier, but is error-prone. The regex is easily extended to grab other parts of the string in one go.