Bart
Bart

Reputation: 134

Regular expression: retrieve one or more numbers after certain text

I'm trying to parse HTML code and extra some data from with using regular expressions. The website that provides the data has no API and I want to show this data in an iOS app build using Swift. The HTML looks like this:

$(document).ready(function() {

var years = ['2020','2021','2022'];
var currentView = 0;
var amounts = [1269.2358,1456.557,1546.8768];
var balances = [3484626,3683646,3683070];

rest of the html code

What I'm trying to extract is the years, amounts and balances.

So I would like to have an array with the year in in [2020,2021,2022] same for amount and balances. In this example there are 3 years, but it could be more or less. I'm able to extra all the numbers but then I'm unable to link them to the years or amounts or balances. See this example: https://regex101.com/r/WMwUji/1, using this pattern (\d|\[(\d|,\s*)*])

Any help would be really appreciated.

Upvotes: 0

Views: 82

Answers (2)

schlin
schlin

Reputation: 369

Firstly I think there are some errors in your expression. To capture the whole number you have to use \d+ (which matches 1 or more consecutive numbers e.g. 2020). If you need to include . as a separator the expression then would look like \d+\.\d+. In addition using non-capturing group, (?:) and non-greedy matches .*? the regular-expression that gives the desired result for years is

(?:year.*?|',')(\d+)

This can also be modified for the amount field which would look like this:

(?:amounts.*?|,)(\d+\.\d+)

You can try it here: https://regex101.com/r/QLcFQN/1

Edited: in the previous Version my proposed regex was non functional and only captured the last match.

Upvotes: 1

Gschmaaz
Gschmaaz

Reputation: 91

You can continue with this regex:

^var (years \= (?'year'.*)|balances \= (?'balances'.*)|amounts \= (?'amounts'.*));$

It searches for lines with either years, balances or amount entries and names the matches acordingly. It matches the whole string within the brackets.

Upvotes: 0

Related Questions