Tags down


scala - regular expression

By : Mike Y
Date : October 16 2020, 03:08 PM
this will help I am new to scala. I am not new to regular expression, but scala's regular expression is a bit confusing to me. For example, my input variable is string from the "DEVICE" column
code :
val myDevice = "IOSTABLET"

val translated = myDevice match {
   case "SAFARI" | "IE" => "DESKTOP"
   case "etc" | "etc2" => "etc3"

Share : facebook icon twitter icon

scala: get all possible matches of a regular expression

By : Baran Ipek
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I need to find all the pairs of word joined with the "and" word. , Your regex
code :

Scala Regular Expression

By : user2733330
Date : March 29 2020, 07:55 AM
it fixes the issue Simple mistake. * is a greedy operator, meaning it will match as much as it can and still allow the remainder of the regex to match. Use *? instead for a non-greedy match meaning "zero or more — preferably as few as possible".
code :

Matching or regular expression in scala

By : Yao Rao
Date : March 29 2020, 07:55 AM
this will help I have a regular expression like ".*(A20).*|.*(B30).*|C". I would like to write a function its returns A20 or B30 based on the match found. , You're close:
code :
def extract(s: String) = s match {
 case regx(b, _) if b != null => b
 case regx(_, b) if b != null => b
 case _ => "" 

res3: String = A20

res4: String = B30

res6: String = B30
val letters = ('A' to 'Z').toSeq
val regex = letters.map(_.toString).mkString("(", "|", ")").r

def extract(s: String) = {
  for {
    m <- regex.findFirstMatchIn(s)
  } yield m.group(1)


res0: Option[String] = Some(A)
res1: Option[String] = Some(B)
res2: Option[String] = Some(C)
res3: Option[String] = Some(E)

How to use regular expression in scala?

By : Joel
Date : March 29 2020, 07:55 AM
wish of those help The pattern-matching way you are extracting the number is rather resource consuming: since the pattern must match the whole string, you have to add .* on both ends of the regex, and that triggers a lot of backtracking. You also added a space to make sure the first .* does not eat all the digits on the left and return all 1+ digits found.
If you are looking for a first match, use findFirstIn:
code :
val myString: String = "there would be some number here 34."
val numberString = """\d+""".r.findFirstIn(myString)
val num = numberString.get.toInt
println(num) // => 34

Need Regular Expression in scala

By : Robert L. Scheier
Date : March 29 2020, 07:55 AM
around this issue Why are you using regular expressions here? Just read the JSON. JSON is not a regular language, and can not be easily described with regex.
Here's one of many library's that will parse JSON for Scala https://github.com/json4s/json4s
Privacy Policy - Terms - Contact Us © soohba.com