简体   繁体   中英

Scala get a substring

I am trying to get a substring in scala. The string that I have is the following:

fromTo: NT=xxx_bt_bsns_m,OD=ntis,OS=wnd,SX=xs,SZ=ddp,
fromTo: NT=xds_bt2_bswns_m,OD=nis,OS=wnd,SX=xs,SZ=ddp,
fromTo: NT=xxa_bt1_b1ns_m,OD=nts,OS=nd,SX=xs,SZ=ddp

I just want to get a substring with:

 xxx_bt_bsns_m

Edit: This substring can be other, for example ddd_zn1_ldk

So what i have to try to get all the string that start with NT and ends with a "," maybe?

I am starting with scala so for this reason i am having doubts with this.

Thanks in advance!

We could use a regex replacement here:

val input = "fromTo: NT=xxx_bt_bsns_m,OD=ntis,OS=wnd,SX=xs,SZ=ddp"
val output = input.replaceAll("^.*\\bNT=([^,]+).*$", "$1")
println(output)  // xxx_bt_bsns_m

Consider interpolated string patterns

input match {
  case s"fromTo: NT=${nt},${_}" => nt
}

however this will throw an exception if it cannot pattern match the input, so you have to decide what to do in that case. Perhaps you could return empty string

input match {
  case s"fromTo: NT=${nt},${_}" => nt
  case _ => ""
}

or use an Option to model the error case

input match {
  case s"fromTo: NT=${nt},${_}" => Some(nt)
  case _ => None
}

For your example strings, you can first match the pattern, and use a capture group for the part after fromTo:

The split the group 1 value on a comma, and filter the collection for values that start with NT= if you can have multiple values.

In parts, the pattern matches:

  • ^ Start of string
  • fromTo:\\h+ Match fromTo: and 1 or more horizontal spaces
  • ( Capture group 1
    • [AZ]+=[^\\s,=]+ Match 1+ uppercase chars = and 1+ chars other than , = or whitespace chars
    • (?:,[AZ]+=[^\\s,=]+)* Optionally repeat the same perceded by a comma
  • ) Close group 1
  • ,? Match an optional comma
  • $ End of sting

For example

val s = "fromTo: NT=xxa_bt1_b1ns_m,OD=nts,OS=nd,SX=xs,SZ=ddp,NT=aaaaaa"
val pattern = """^fromTo:\h+([A-Z]+=[^\s,=]+(?:,[A-Z]+=[^\s,=]+)*),?$""".r

val result: Option[Array[String]] = pattern.findFirstMatchIn(s).map(m =>
     m.group(1)
    .split(",")
    .filter(s => s.startsWith("NT=")))

result.foreach(a => a.foreach(println))

Output

NT=xxa_bt1_b1ns_m
NT=aaaaaa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM