【问题标题】:Split string based on parantheses根据括号拆分字符串
【发布时间】:2020-07-03 20:58:02
【问题描述】:

我正在编写基于冒号 (:) 分割一行的 Scala 代码。 例如,对于看起来像这样的输入:

sparker0i@outlook.com : password

我在做line.split(" : ")(本质上是Java)并在控制台上打印电子邮件和密码。

现在我的要求已经改变,现在一行看起来像:

(sparker0i@outlook.com,sparker0i) : password

我想分别打印电子邮件用户名密码

我通过首先尝试拆分括号来尝试使用正则表达式,但这不起作用,因为它不正确 (val lt = line.split("[\\\\(||//)]"))。请用正确的正则表达式/拆分逻辑指导我。

【问题讨论】:

    标签: java regex scala split


    【解决方案1】:

    在 scala 2.13 中,有一个没有正则表达式的简单解决方案:

    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
    

    【讨论】:

      【解决方案2】:

      我将使用的正则表达式:

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

      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
      

      Java 实现将是:

      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));
              }
          }
      }
      

      打印:

      sparker0i@outlook.com
      sparker0i
      password
      

      Java Demo

      【讨论】:

        【解决方案3】:

        这没有做太多改变

            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]);
        

        输出

        sparker0i@outlook.com,sparker0i   password
        

        【讨论】:

          【解决方案4】:

          我不是 scala 用户,但我认为您可以使用 Pattern 和匹配器来提取此信息,而不是拆分,您的正则表达式可以使用以下组:

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

          regex demo

          然后您可以提取第 1 组作为电子邮件,第 2 组作为用户名,第 3 组作为密码。

          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))
          }
          

          感谢这篇文章https://stackoverflow.com/a/3051206/5558072

          【讨论】:

          • 也可以使用input match { case pattern(email, name, password) => /* ... */ }进行匹配。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-24
          • 2014-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-12
          • 1970-01-01
          相关资源
          最近更新 更多