[英]How to use regex from JShell?
我可以使用.java
文件获取 output ,如下所示,而且,是的, JShell
可以加载 class 文件,我更感兴趣的是使用下面的脚本。
如何使用JShell
控制台生成类似的 output?
简单的正则表达式示例:
[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));
}
}
}
}
尝试在 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
方法中获得类似的 output ,但从JShell
本身获得?
解决方案:
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()
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.