Scala get a substring

Viewed 112

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!

2 Answers

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

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.

fromTo:\h+([A-Z]+=[^\s,=]+(?:,[A-Z]+=[^\s,=]+)*)

Regex demo

In parts, the pattern matches:

  • fromTo:\h+ Match fromTo: and 1 or more horizontal spaces
  • ( Capture group 1
    • [A-Z]+=[^\s,=]+ Match 1+ uppercase chars = and 1+ chars other than , = or whitespace chars
    • (?:,[A-Z]+=[^\s,=]+)* Optionally repeat the same perceded by a comma
  • ) Close group 1

For example

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

val result: Iterator[Array[String]] = pattern.findAllMatchIn(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
NT=bbbbbbbbb
Related