Anisotropic
Anisotropic

Reputation: 645

Regex on nested arrays?

I've got a string of text that looks like such:

...],null,null,
],
["Tuesday",["8AM–5:30PM"]
,null,null,"2018-09-25",1,[[8,0,17,30]
]
,0]
,["Wednesday",["8AM–5:30PM"]
,null,null,"2018-09-26",1,[[8,0,17,30]
]
,0]
,["Thursday",["8AM–5:30PM"]
,null,null,"2018-09-27",1,[[8,0,17,30]
],x,y,[[[.....

I know this ends with three consecutive left brackets.

I'm writing a regex to grab all the arrays starting from the first day to the end of the array of the last day, but I'm having trouble getting too much returned.

val regEx = """[a-zA-Z]*(day)(?s)(.*)(\[\[\[\")""".r

I'm using the (?s)(.*) to capture the fact that there can be newlines between day arrays.

This is essentially grabbing everything from the text following the first day rather than stopping at the [[[.

How can I resolve this issue?

Upvotes: 0

Views: 524

Answers (2)

RAGHHURAAMM
RAGHHURAAMM

Reputation: 1099

I know this ends with three consecutive left brackets.

I'm writing a regex to grab this, but having trouble getting too much returned

If you just need to grab that [[[, it can be done as below:

 val str = """Tuesday",["8AM?5:30PM"]
 ,null,null,"2018-09-25",1,[[8,0,17,30]
 ]
 ,0]
 ,["Wednesday",["8AM?5:30PM"]
 ,null,null,"2018-09-26",1,[[8,0,17,30]
 ]
 ,0]
 ,["Thursday",["8AM?5:30PM"]
 ,null,null,"2018-09-27",1,[[8,0,17,30]
 ],x,y,[[[....."""

scala> val regEx = """\[\[\[""".r
regEx: scala.util.matching.Regex = \[\[\[

scala> regEx.findFirstIn(str).get
res20: String = [[[

If you have more [[[ in the str, you can use, regEx.findAllIn(str).toArray which returns
an Array("[[[",....)

scala> regEx.findAllIn(str).toArray
res22: Array[String] = Array([[[)

Upvotes: 0

jwvh
jwvh

Reputation: 51271

Scala regex defaults to anchored, but your text string doesn't end with the target [[[. There's more after that so you want it unanchored.

You put the text day in a capture group, which seems rather pointless in that you're losing the part that identifies which day you're starting with.

Why put the closing [[[ in a capture group? I don't see its purpose.

Your regex pattern ends with a single quote, ", but that's not in the sample string so this pattern won't match at all, even though you claim it's "grabbing everything ... rather than stopping at the [[[". You should make sure that the code you post fails in the way you describe.

The title of you question mentions "nested arrays" but there are no arrays, nested or otherwise. You have a String that you are trying to parse. Perhaps something like this:

val str = """Tuesday",["8AM–5:30PM"]
,null,null,"2018-09-25",1,[[8,0,17,30]
]
,0]
,["Wednesday",["8AM–5:30PM"]
,null,null,"2018-09-26",1,[[8,0,17,30]
]
,0]
,["Thursday",["8AM–5:30PM"]
,null,null,"2018-09-27",1,[[8,0,17,30]
],x,y,[[[....."""

val regEx = """([a-zA-Z]*day)(?s)(.*)\[\[\[""".r.unanchored

str match {
  case regEx(a,b) => s"-->>$a$b<<--"
  case _ => "nope"
}

Upvotes: 1

Related Questions