Java正则表达式-从匹配文本中获取行号
这是基于我的 对于我的例子,我想从正则表达式模式中得到行数。例如:Java正则表达式-从匹配文本中获取行号,java,regex,Java,Regex,这是基于我的 对于我的例子,我想从正则表达式模式中得到行数。例如: name : andy birth : jakarta, 1 jan 1990 number id : 01011990 01 age : 26 study : Informatics engineering 我想从与数字[0-9]+匹配的文本中获取行号。我希望输出如下: line 2 line 3 line 4 使用扫描仪迭代输入的所有行。并使用Matcher对象检查正则表达式模式 String s = "name : a
name : andy
birth : jakarta, 1 jan 1990
number id : 01011990 01
age : 26
study : Informatics engineering
我想从与数字[0-9]+
匹配的文本中获取行号。我希望输出如下:
line 2
line 3
line 4
使用
扫描仪
迭代输入的所有行。并使用Matcher
对象检查正则表达式模式
String s = "name : andy\n" +
"birth : jakarta, 1 jan 1990\n" +
"number id : 01011990 01\n" +
"age : 26\n" +
"study : Informatics engineering";
Scanner sc = new Scanner(s);
int lineNr = 1;
while (sc.hasNextLine()) {
String line = sc.nextLine();
Matcher m = Pattern.compile(".*[0-9].*").matcher(line);
if(m.matches()){
System.out.println("line " + lineNr);
}
lineNr++;
}
public static void main( String[] args )
{
String s = "name : andy\n" + "birth : jakarta, 1 jan 1990\n" + "number id : 01011990 01\n" + "age : 26\n"
+ "study : Informatics engineering";
try
{
Pattern pattern = Pattern.compile( ".*[0-9].*" );
Matcher matcher = pattern.matcher( s );
int line = 1;
while ( matcher.find() )
{
line++;
System.out.println( "line :" + line );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
您可以简单地拥有以下内容:
public static void main(String[] args) throws IOException {
int i = 1;
Pattern pattern = Pattern.compile(".*[0-9]+.*");
try (BufferedReader br = new BufferedReader(new FileReader("..."))) {
String line;
while ((line = br.readLine()) != null) {
if (pattern.matcher(line).matches()) {
System.out.println("line " + i);
}
i++;
}
}
}
这段代码只是打开一个指向给定文件路径的文件,并在其中的每一行上迭代(直到返回
null
,表示文件结束)。如果该行与模式“*[0-9]+.*”
匹配,表示该行至少包含一个数字,则会打印行号。这将为您解决此问题。我将正则表达式修改为“*[0-9].*”
import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.stream.stream;
导入java.util.regex.Pattern;
导入java.util.concurrent.AtomicInteger;
类正则表达式行
{
公共静态void main(字符串[]args)
{
新建RegExLine().run();
}
公开募捐
{
String fileName=“C:\\Path\\to\\input\\file.txt”;
AtomicInteger AtomicInteger=新的AtomicInteger(0);
try(Stream=Files.line(path.get(fileName)))
{
stream.forEach(s->
{
atomicInteger.getAndIncrement();
if(Pattern.matches(“%0-9]”,s))
{
System.out.println(“行”+原子整数);
}
});
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
使用Matcher对象检查正则表达式模式
String s = "name : andy\n" +
"birth : jakarta, 1 jan 1990\n" +
"number id : 01011990 01\n" +
"age : 26\n" +
"study : Informatics engineering";
Scanner sc = new Scanner(s);
int lineNr = 1;
while (sc.hasNextLine()) {
String line = sc.nextLine();
Matcher m = Pattern.compile(".*[0-9].*").matcher(line);
if(m.matches()){
System.out.println("line " + lineNr);
}
lineNr++;
}
public static void main( String[] args )
{
String s = "name : andy\n" + "birth : jakarta, 1 jan 1990\n" + "number id : 01011990 01\n" + "age : 26\n"
+ "study : Informatics engineering";
try
{
Pattern pattern = Pattern.compile( ".*[0-9].*" );
Matcher matcher = pattern.matcher( s );
int line = 1;
while ( matcher.find() )
{
line++;
System.out.println( "line :" + line );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
你能详细说明你的问题吗?你说的台词在哪里?为什么要使用正则表达式?包含数字(十进制)的行。你有最好的方法吗?如果你想提取所有包含数字的行,请参阅下面的答案。如何不逐行匹配?如果我的文本太多,时间会很长。如何避免逐行匹配?如果我有大量的文本,时间会很长。@新手你想打印行号,所以你必须检查每一行,你无法提前知道一行是否匹配。小而简单-必须是Java 8;=)注意使用正确的JDK@newbie@newbie请你接受我的回答,或者澄清你的问题。谢谢