3

I have a string in Scala. I am trying to find all occurrences of a certain tag and find the text that is enclosed in the tag, storing the results in an array.

So for example, if my string is val string = "<p> c </p> <p> a </p> <p> t </p>" The result I am looking for is:

val result = ["c","a","t"]

What is the best way to do this? Thanks!
Note: I am certain the tag type I am searching for will never appear nested inside another instance of itself, if that helps.

2 Answers 2

4

Here's a slight modification to Tony's suggestion.

val string = "<p> c </p> <p> a </p> <p> t </p>"
val re = "(?<=<p>)([^<]*)(?=</p>)".r
re.findAllMatchIn(string).map(_.toString.trim).toArray
// res0: Array[String] = Array(c, a, t)

The .trim part is optional.

Sign up to request clarification or add additional context in comments.

Comments

0

The following expression will match all letters between <p> and </p>.

(?<=<p>)[^<>]*(?=<\/p>)

Your list of matches should produce the result you are looking for.

Try the following to get a list of matches:

val reg = "(?<=<p>)[^<>]*(?=<\/p>)".r
val str = "<p> c </p> <p> a </p> <p> t </p>"
val matches = reg.findAllIn(str)

while (matches.hasNext) {

    val match = matches.next

}

6 Comments

How can I use this regex to actually create the array I want? Sorry, I am quite new to Scala.
@user308485 I added an example to my answer, however I also have no experience with scala and am unsure if it would work.
Just tested. It seems to compile, but doesn't seem to produce the expected result, just an empty iterator.
@user308485 Try the loop I added to the answer. Could you loop through the matches and add the data to an array?
I can try, but this would be considered poor practice in a functional language like Scala.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.