Scala: detect cyrillic and latin characters in string? -


please simple scala method allows to:

  1. detect if string contains @ least 1 latin character
  2. detect if string characters latin
  3. detect if string contains @ least 1 cyrillic character
  4. detect if string characters cyrillic

so far tried:

scala> val pattern = new regex("[a-za-z]") pattern: scala.util.matching.regex = [a-za-z] scala> val s = "john" s: string = john scala> pattern findfirstin s res22: option[string] = some(j) 

thanks!

here go

1. 1 latin char

scala> ("[a-za-z]".r findfirstin "munich").isdefined res22: boolean = true 

2. latin char

scala> "munich".tolist.forall(c => ( c >= 'a' && c<= 'z') || (c >= 'a' && c <= 'z') ) res27: boolean = true 

3. @ least 1 cyrillic char:

("\\p{iscyrillic}".r findfirstin "Москва").isdefined res5: boolean = true 

4. chars cyrillic:

val moscow = "Москва"  "\\p{iscyrillic}*".r.findfirstin(moscow).map(_.size) == some(moscow.size) res21: boolean = true 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -