Reputation: 37
Using Scala, I am trying to extract a value from a path e.g.
val path = hdfs://aloha/data/8907yhb/folders/folder=2319/
What regex can I use for [extracting part]
I have tried it at https://regexr.com/
[]
(https://i.sstatic.net/pvgVX.png)
"=[0-9]*"
I didn't get exactly what I expected.
The best result I got:
=2319
but I need to get only
2319
Upvotes: 2
Views: 119
Reputation: 163362
You can use a capture group, and repeat matching the digits 1 or more times to prevent empty matches:
=([0-9]+)
See a regex demo and a Scala demo.
For example:
val path = "hdfs://aloha/data/8907yhb/folders/folder=2319/"
val pattern = "=([0-9]+)".r
val result = pattern.findFirstMatchIn(path) match {
case Some(v) => v.group(1)
case _ => "No match"
}
println(result)
Or with \d
to match a digit:
val pattern = "=(\\d+)".r
Output
2319
Upvotes: 1
Reputation: 7926
You can use look behind (?<=...)
pattern.
(?<==)[0-9]* // matches zero or more digits but only if they follow the '=' sign.
Example:
scala> "(?<==)[0-9]*".r.findAllIn("hdfs://aloha/data/8907yhb/folders/folder=2319/").toList
List(2319)
Upvotes: 2