Eamo
Eamo

Reputation: 39

Using RegEx to extract digital number from the innertext of a webpage element

I used the regex of (?:\[+\.)?\d+ to extract the digits from the innertext of the website element, the innertext was $94.99/mo.

This worked and I can create a variable equal to 105.66. When I apply this regex to innertext which has different digital values contained in it, than all the digital values are selected.

e.g.

Total Monthly Payment: $94.99 + $9.49 (for ASP) = $104.48/month (plus tax) • Total Cost of Ownership: $104.48 x 12 Months = $1,253.76 (plus tax) • Cost of Lease Services: $350.77

It finds all the values; How do I modify the regex so that it only finds the 9.49 price?

Upvotes: 2

Views: 212

Answers (1)

Alexander Derck
Alexander Derck

Reputation: 14498

I would try to extract this: + $9.49 You can use following regex:

@"(?<=\+\s\$)(\d+\.?\d*)
  • (?<=\+\s\$) match but don't include a + followed by a whitespace, followed by a $
  • (\d+\.?\d*) match and put in a group at least one digit, followed by an optional . followed by any number of digits.

The + . $ signs are special regex characters which have to be escaped with a backslash.

Upvotes: 1

Related Questions