有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用JShell中的正则表达式?

我可以使用.java文件获得输出,如下所示,虽然JShell可以加载类文件,但我更感兴趣的是将其用作一种脚本REPL,如下所示

如何使用JShell控制台生成类似的输出

simple正则表达式示例:

[nsaunders@rolly Hands-On-Java-Regular-Expressions]$ 
[nsaunders@rolly Hands-On-Java-Regular-Expressions]$ gradle run

> Task :run
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 1         John
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 2         writes
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 1         John
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 2         Doe
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 1         John
Jun. 14, 2020 3:54:53 P.M. com.jimbotec.dc.regex.MyRegex match
INFO: 2         Wayne

BUILD SUCCESSFUL in 1s
3 actionable tasks: 1 executed, 2 up-to-date
[nsaunders@rolly Hands-On-Java-Regular-Expressions]$ 

爪哇:

package com.jimbotec.dc.regex;

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

public class MyRegex {

    Logger log = Logger.getLogger(App.class.getName());

    public void match() {

        String text
                = "John writes about this, and John Doe writes about that,"
                + " and John Wayne writes about everything.";

        String patternString1 = "(John) (.+?) ";

        Pattern pattern = Pattern.compile(patternString1);
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            for (int i = 1; i <= matcher.groupCount(); i++) {
                log.info(i + "\t\t" + matcher.group(i));
            }
        }
    }
}

尝试在JShell中复制上述代码:

jshell> /reset
|  Resetting state.

jshell> /open src/main/resources/regex.jsh
|  Exception java.lang.IllegalStateException: No match found
|        at Matcher.group (Matcher.java:645)
|        at (#6:1)

jshell> /list

   1 : import static java.lang.System.out;
   2 : String text = "John writes about this, and John writes about that, and John writes about everything.";
   3 : String patternString1 = "(John) (.+?)";
   4 : Pattern pattern = Pattern.compile(patternString1);
   5 : Matcher matcher = pattern.matcher(text);
   6 : out.println(matcher.group(1));
jshell> 

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
|    import static java.lang.System.out

jshell> 

我怎样才能像上面的match方法那样从JShell本身内部获得similar输出


共 (1) 个答案

  1. # 1 楼答案

    解决方案:

    jshell> 
    
    jshell> /reset
    |  Resetting state.
    
    jshell> 
    
    jshell> 
    
    jshell> /open regex.jsh
    John w
    John
    w
    
    jshell> 
    
    jshell> /list
    
       1 : import static java.lang.System.out;
       2 : String text = "John writes about this, and John writes about that, and John writes about everything.";
       3 : String patternString1 = "(John) (.+?)";
       4 : Pattern pattern = Pattern.compile(patternString1);
       5 : Matcher matcher = pattern.matcher(text);
       6 : matcher.find()
       7 : out.println(matcher.group(0))
       8 : out.println(matcher.group(1))
       9 : out.println(matcher.group(2))
    
    jshell> 
    

    只是忘了运行find()