david hol
david hol

Reputation: 1280

How to parse HTML element?

I have long string with Currency that look like the following :

...<option value="USD">USD - United States Dollar</option>    <option value="JPY">JPY - Japanese Yen</option>...

What is the fastest way in order to extract 2 values:

USD
USD - United States Dollar

Upvotes: 0

Views: 230

Answers (1)

Assaf
Assaf

Reputation: 1370

If it's really just getting certain substrings out of a string then I'd go with a regex here.

Use a capturing group (make sure it's not greedy) to get the parts of the string that interest you (in this case the value property and the tag content).

val str =
  """<option value="USD">USD - United States Dollar</option><option value="JPY">JPY - Japanese Yen</option>"""
val pattern = """<option value="(.+?)">(.+?)</option>""".r

pattern.findAllMatchIn(str).foreach(x => println(x.group(1) + " " + x.group(2)))
/* output:
 * USD USD - United States Dollar
 * JPY JPY - Japanese Yen
 */

Upvotes: 1

Related Questions