Split string based on parantheses

Sparker0i :

I'm writing Scala code which splits a line based on a colon (:). Example, for an input which looked like:

[email protected] : password

I was doing line.split(" : ") (which is essentially Java) and printing the email and the password on Console.

Now my requirement has changed and now a line will look like:

([email protected],sparker0i) : password

I want to individually print the email, username and password separately.

I've tried Regex by first trying to split the parantheses, but that didn't work because it is not correct (val lt = line.split("[\\\\(||//)]")). Please guide me with the correct regex/split logic.

YCF_L :

I'm not a scala user, but instead of split, I think you can use Pattern and matcher to extract this info, your regex can use groups like:

\((.*?),(.*?)\) : (.*)

regex demo

Then you can extract group 1 for email, group 2 for username and the 3rd group for password.

val input = "([email protected],sparker0i) : password"
val pattern = """\((.*?),(.*?)\) : (.*)""".r
pattern.findAllIn(string).matchData foreach {
   m => println(m.group(1) + " " + m.group(2) + " " + m.group(3))
}

Credit for this post https://stackoverflow.com/a/3051206/5558072

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349181&siteId=1
Recommended