If you want to get the first match (as there will always be one match in the string), it makes more sense to use findFirstIn:
"""(?<=PersonId:)[^;]+""".r.findFirstIn(str).get
The (?<=PersonId:)[^;]+ regex means:
(?<=PersonId:) - assert there is PersonId: text immediately to the left of the current position
[^;]+ - 1+ chars other than ;
See the regex demo.
See the Scala demo:
val str = "bla bla bla PersonId:fruhdHH$skdjJIFROfUB3djeggG$tt; bla bla bla"
val personIdRegex = """(?<=PersonId:)[^;]+""".r
val personIdExtracted = personIdRegex.findFirstIn(str).get
println(personIdExtracted)
// => fruhdHH$skdjJIFROfUB3djeggG$tt
Or, a more natural way, use match block with an unanchored regex (here, you may match optional whitespace between PersonId: and the ID itself without restrictions):
val personIdRegex = """PersonId:\s*([^;]+)""".r.unanchored
val personIdExtracted = str match {
case personIdRegex(person_id) => person_id
case _ => ""
}
See this Scala demo.
Here, the .unanchored makes the pattern match partial substrings inside a string, and ([^;]+) in the regex forms a capturing group that can be referred to by any arbitrary name inside match block (I chose person_id).
EntryIDwhile your example string containsPersonID. Is that just a silly mistake, am I right?