简体   繁体   中英

How to use regex from JShell?

I'm able to get output using .java files as below, and, while, yes, JShell can load class files, I'm more interested in using it as a sort of scripting REPL as below.

How can I generate similar output using the JShell console?

simple regex example:

[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]$ 

java:

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

Trying to replicate the above code in 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> 

How can I get similar output as in the match method above, but from within JShell itself?

Solution:

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> 

just forgot to run find() ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM