1

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

sparker0i@outlook.com : 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:

(sparker0i@outlook.com,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.

gregorycallea
  • 1,218
  • 1
  • 9
  • 28
Sparker0i
  • 1,787
  • 4
  • 35
  • 60

4 Answers4

4

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 = "(sparker0i@outlook.com,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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • You can also use `input match { case pattern(email, name, password) => /* ... */ }` to match. – Kolmar Mar 23 '20 at 17:13
1

The regex I would use:

\((.*?),([^)]+)\) : (.+)

Regex Demo

\(        # Matches (
(         # Start of capture group 1
   (.*?)  # Capture 0 or more characters until ...
)         # End of capture group 1
,         # matches ,
(         # start of capture group 2
   [^)]+  # captures one or more characters that are not a )
)         # end of capture group 2
\)        # Matches )
 :        # matches ' : '
(         # start of capture group 3
   (.+)   # matches rest of string
)         # end of capture group 3

The Java implementation would be:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Test
{

    public static void main(String[] args) {
        String s =  "(sparker0i@outlook.com,sparker0i) : password";
        Pattern pattern = Pattern.compile("\\((.*?),([^)]+)\\) : (.+)");
        Matcher m = pattern.matcher(s);
        if (m.matches()) {
            System.out.println(m.group(1));
            System.out.println(m.group(2));
            System.out.println(m.group(3));
        }
    }
}

Prints:

sparker0i@outlook.com
sparker0i
password

Java Demo

Booboo
  • 38,656
  • 3
  • 37
  • 60
1

In scala 2.13, there is a simple solution without regrex:

Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_222).
Type in expressions for evaluation. Or try :help.

scala> val input = "(sparker0i@outlook.com,sparker0i) : password"
input: String = (sparker0i@outlook.com,sparker0i) : password

scala> val s"($mail,$user) : $pwd" = input
mail: String = sparker0i@outlook.com
user: String = sparker0i
pwd: String = password
esse
  • 1,415
  • 5
  • 10
0

this is without doing much change

    String s =  "(sparker0i@outlook.com,sparker0i) : password";
    // do whatever you were doing 
    String[] sArr = s.split(":");
    sArr[0] = sArr[0].replaceAll("[(|)]","");  // just replace those parenthesis with empty string
    System.out.println(sArr[0] + " " + sArr[1]);

Output

sparker0i@outlook.com,sparker0i   password
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24