Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select substring between two characters in Scala

Tags:

string

scala

I'm getting a garbled JSON string from a HTTP request, so I'm looking for a temp solution to select the JSON string only.

The request.params() returns this:

[{"insured_initials":"Tt","insured_surname":"Test"}=, _=1329793147757,
callback=jQuery1707229194729661704_1329793018352

I would like everything from the start of the '{' to the end of the '}'.

I found lots of examples of doing similar things with other languages, but the purpose of this is not to only solve the problem, but also to learn Scala. Will someone please show me how to select that {....} part?

like image 864
Jack Avatar asked Feb 21 '12 07:02

Jack


People also ask

How do I extract a string between two characters?

To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key. Note: A1 is the text cell, > and < are the two characters you want to extract string between.

How do I extract a string between two characters in Python?

Using index() + loop to extract string between two substrings. In this, we get the indices of both the substrings using index(), then a loop is used to iterate within the index to find the required string between them.


1 Answers

Regexps should do the trick:

"\\{.*\\}".r.findFirstIn("your json string here")
like image 183
Jens Schauder Avatar answered Sep 20 '22 18:09

Jens Schauder