Java 用于提取文本的正则表达式
我有这样一个文本:Java 用于提取文本的正则表达式,java,regex,Java,Regex,我有这样一个文本: SrcAddr: 0.0.21.201 DstAddr: 7.202.10.100 NextHop: 0.33.189.142 InputIf: 19 OutputIf: 50715 我想使用regex提取这样的数据 String SrcAddr = "0.0.21.201"; String DstAddr = "7.202.10.100"; //ect... 我试过各种各样的表达方式,但还是不走运。如果有人能帮你欣赏很多东西,你可以试
SrcAddr: 0.0.21.201
DstAddr: 7.202.10.100
NextHop: 0.33.189.142
InputIf: 19
OutputIf: 50715
我想使用regex提取这样的数据
String SrcAddr = "0.0.21.201";
String DstAddr = "7.202.10.100";
//ect...
我试过各种各样的表达方式,但还是不走运。如果有人能帮你欣赏很多东西,你可以试试:
String in = " SrcAddr: 0.0.21.201\nDstAddr: 7.202.10.100\n NextHop: 0.33.189.142\n InputIf: 19\n OutputIf: 50715";
Pattern src = Pattern.compile("SrcAddr:\\s+([\\d\\.]+)");
Pattern dst = Pattern.compile("DstAddr:\\s+([\\d\\.]+)");
Pattern nextHop = Pattern.compile("NextHop:\\s+([\\d\\.]+)");
Pattern inputIf = Pattern.compile("InputIf:\\s+([\\d]+)");
Pattern outputIf = Pattern.compile("OutputIf:\\s+([\\d]+)");
Matcher srcMatcher = src.matcher(in);
Matcher dtsMatcher = dst.matcher(in);
Matcher nextHopMatcher = nextHop.matcher(in);
Matcher inputIfMatcher = inputIf.matcher(in);
Matcher outputIfMatcher = outputIf.matcher(in);
if (srcMatcher.find()) {
System.out.println(srcMatcher.group(1));
}
if (dtsMatcher.find()) {
System.out.println(dtsMatcher.group(1));
}
if (nextHopMatcher.find()) {
System.out.println(nextHopMatcher.group(1));
}
if (inputIfMatcher.find()) {
System.out.println(inputIfMatcher.group(1));
}
if (outputIfMatcher.find()) {
System.out.println(outputIfMatcher.group(1));
}
希望有帮助 您可以尝试:
String in = " SrcAddr: 0.0.21.201\nDstAddr: 7.202.10.100\n NextHop: 0.33.189.142\n InputIf: 19\n OutputIf: 50715";
Pattern src = Pattern.compile("SrcAddr:\\s+([\\d\\.]+)");
Pattern dst = Pattern.compile("DstAddr:\\s+([\\d\\.]+)");
Pattern nextHop = Pattern.compile("NextHop:\\s+([\\d\\.]+)");
Pattern inputIf = Pattern.compile("InputIf:\\s+([\\d]+)");
Pattern outputIf = Pattern.compile("OutputIf:\\s+([\\d]+)");
Matcher srcMatcher = src.matcher(in);
Matcher dtsMatcher = dst.matcher(in);
Matcher nextHopMatcher = nextHop.matcher(in);
Matcher inputIfMatcher = inputIf.matcher(in);
Matcher outputIfMatcher = outputIf.matcher(in);
if (srcMatcher.find()) {
System.out.println(srcMatcher.group(1));
}
if (dtsMatcher.find()) {
System.out.println(dtsMatcher.group(1));
}
if (nextHopMatcher.find()) {
System.out.println(nextHopMatcher.group(1));
}
if (inputIfMatcher.find()) {
System.out.println(inputIfMatcher.group(1));
}
if (outputIfMatcher.find()) {
System.out.println(outputIfMatcher.group(1));
}
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
希望有帮助 您可以尝试:
String in = " SrcAddr: 0.0.21.201\nDstAddr: 7.202.10.100\n NextHop: 0.33.189.142\n InputIf: 19\n OutputIf: 50715";
Pattern src = Pattern.compile("SrcAddr:\\s+([\\d\\.]+)");
Pattern dst = Pattern.compile("DstAddr:\\s+([\\d\\.]+)");
Pattern nextHop = Pattern.compile("NextHop:\\s+([\\d\\.]+)");
Pattern inputIf = Pattern.compile("InputIf:\\s+([\\d]+)");
Pattern outputIf = Pattern.compile("OutputIf:\\s+([\\d]+)");
Matcher srcMatcher = src.matcher(in);
Matcher dtsMatcher = dst.matcher(in);
Matcher nextHopMatcher = nextHop.matcher(in);
Matcher inputIfMatcher = inputIf.matcher(in);
Matcher outputIfMatcher = outputIf.matcher(in);
if (srcMatcher.find()) {
System.out.println(srcMatcher.group(1));
}
if (dtsMatcher.find()) {
System.out.println(dtsMatcher.group(1));
}
if (nextHopMatcher.find()) {
System.out.println(nextHopMatcher.group(1));
}
if (inputIfMatcher.find()) {
System.out.println(inputIfMatcher.group(1));
}
if (outputIfMatcher.find()) {
System.out.println(outputIfMatcher.group(1));
}
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
希望有帮助 您可以尝试:
String in = " SrcAddr: 0.0.21.201\nDstAddr: 7.202.10.100\n NextHop: 0.33.189.142\n InputIf: 19\n OutputIf: 50715";
Pattern src = Pattern.compile("SrcAddr:\\s+([\\d\\.]+)");
Pattern dst = Pattern.compile("DstAddr:\\s+([\\d\\.]+)");
Pattern nextHop = Pattern.compile("NextHop:\\s+([\\d\\.]+)");
Pattern inputIf = Pattern.compile("InputIf:\\s+([\\d]+)");
Pattern outputIf = Pattern.compile("OutputIf:\\s+([\\d]+)");
Matcher srcMatcher = src.matcher(in);
Matcher dtsMatcher = dst.matcher(in);
Matcher nextHopMatcher = nextHop.matcher(in);
Matcher inputIfMatcher = inputIf.matcher(in);
Matcher outputIfMatcher = outputIf.matcher(in);
if (srcMatcher.find()) {
System.out.println(srcMatcher.group(1));
}
if (dtsMatcher.find()) {
System.out.println(dtsMatcher.group(1));
}
if (nextHopMatcher.find()) {
System.out.println(nextHopMatcher.group(1));
}
if (inputIfMatcher.find()) {
System.out.println(inputIfMatcher.group(1));
}
if (outputIfMatcher.find()) {
System.out.println(outputIfMatcher.group(1));
}
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
希望有帮助
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
印刷品:
0.0.21.201
7.202.10.100
印刷品:
0.0.21.201
7.202.10.100
印刷品:
0.0.21.201
7.202.10.100
印刷品:
0.0.21.201
7.202.10.100
查看此在线java正则表达式测试仪: 我将文本粘贴到“input 1”,然后放入正则表达式:
\d*[.]\d*[.]\d*[.]\d*
结果:
[10,20] 0.0.21.201
[32,44] 7.202.10.100
[56,68] 0.33.189.142
不要忘记-在java代码中使用它时,需要避开反斜杠:
"\\d*[.]\\d*[.]\\d*[.]\\d*"
此外,这里还有一个有趣的正则表达式游戏,帮助您学习正则表达式:
查看此在线java正则表达式测试仪: 我将文本粘贴到“input 1”,然后放入正则表达式:
\d*[.]\d*[.]\d*[.]\d*
结果:
[10,20] 0.0.21.201
[32,44] 7.202.10.100
[56,68] 0.33.189.142
不要忘记-在java代码中使用它时,需要避开反斜杠:
"\\d*[.]\\d*[.]\\d*[.]\\d*"
此外,这里还有一个有趣的正则表达式游戏,帮助您学习正则表达式:
查看此在线java正则表达式测试仪: 我将文本粘贴到“input 1”,然后放入正则表达式:
\d*[.]\d*[.]\d*[.]\d*
结果:
[10,20] 0.0.21.201
[32,44] 7.202.10.100
[56,68] 0.33.189.142
不要忘记-在java代码中使用它时,需要避开反斜杠:
"\\d*[.]\\d*[.]\\d*[.]\\d*"
此外,这里还有一个有趣的正则表达式游戏,帮助您学习正则表达式:
查看此在线java正则表达式测试仪: 我将文本粘贴到“input 1”,然后放入正则表达式:
\d*[.]\d*[.]\d*[.]\d*
结果:
[10,20] 0.0.21.201
[32,44] 7.202.10.100
[56,68] 0.33.189.142
不要忘记-在java代码中使用它时,需要避开反斜杠:
"\\d*[.]\\d*[.]\\d*[.]\\d*"
此外,这里还有一个有趣的正则表达式游戏,帮助您学习正则表达式:
您可以使用以下内容:
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr=getMatchedString("SrcAddr",input);
String NextHop=getMatchedString("NextHop",input);
String InputIf=getMatchedString("InputIf",input);
String OutputIf=getMatchedString("OutputIf",input);
System.out.println(SrcAddr);
System.out.println(NextHop);
System.out.println(InputIf);
System.out.println(OutputIf);
..........
public static String getMatchedString(String word,String input){
String REGEX = "(?:"+word+":)\\s(.*)";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
输出
0.0.21.201
0.33.189.142
19
50715
您可以使用以下内容:
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr=getMatchedString("SrcAddr",input);
String NextHop=getMatchedString("NextHop",input);
String InputIf=getMatchedString("InputIf",input);
String OutputIf=getMatchedString("OutputIf",input);
System.out.println(SrcAddr);
System.out.println(NextHop);
System.out.println(InputIf);
System.out.println(OutputIf);
..........
public static String getMatchedString(String word,String input){
String REGEX = "(?:"+word+":)\\s(.*)";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
输出
0.0.21.201
0.33.189.142
19
50715
您可以使用以下内容:
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr=getMatchedString("SrcAddr",input);
String NextHop=getMatchedString("NextHop",input);
String InputIf=getMatchedString("InputIf",input);
String OutputIf=getMatchedString("OutputIf",input);
System.out.println(SrcAddr);
System.out.println(NextHop);
System.out.println(InputIf);
System.out.println(OutputIf);
..........
public static String getMatchedString(String word,String input){
String REGEX = "(?:"+word+":)\\s(.*)";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
输出
0.0.21.201
0.33.189.142
19
50715
您可以使用以下内容:
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr=getMatchedString("SrcAddr",input);
String NextHop=getMatchedString("NextHop",input);
String InputIf=getMatchedString("InputIf",input);
String OutputIf=getMatchedString("OutputIf",input);
System.out.println(SrcAddr);
System.out.println(NextHop);
System.out.println(InputIf);
System.out.println(OutputIf);
..........
public static String getMatchedString(String word,String input){
String REGEX = "(?:"+word+":)\\s(.*)";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
输出
0.0.21.201
0.33.189.142
19
50715
试一试,它将包含多行的字符串作为输入,并逐行输出数字(从外观上看是IP和端口) 因为问题本身就是正则表达式,所以我在阅读过程中没有花太多精力,所以我只使用了扫描仪,没有费心读/写文本文件,我想你已经得到了这一部分
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainRegex {
/**
* @param args
*/
public static void main(String[] args) {
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String output = matchString(line);
System.out.println(output);
}
scanner.close();
}
public static String matchString(String input){
String regex = "(?:\\w*: )((?:\\d+\\.?)*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
}
输出:
0.0.21.201
7.202.10.100
0.33.189.142
19
50715
在扫描仪循环中,您可以处理该行。我无法理解你的问题,如果你只想得到数字,或者用字符串[name]=“[number]”替换每一行。如果是这种情况,您只需在扫描器的循环中篡改一点,并删除正则表达式中的非捕获组(这意味着删除\w之前的“?:”),以便它也捕获单词
希望有帮助 试一试,它将包含多行的字符串作为输入,并逐行输出数字(从外观上看是IP和端口)
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
因为问题本身就是正则表达式,所以我在阅读过程中没有花太多精力,所以我只使用了扫描仪,没有费心读/写文本文件,我想你已经得到了这一部分
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainRegex {
/**
* @param args
*/
public static void main(String[] args) {
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String output = matchString(line);
System.out.println(output);
}
scanner.close();
}
public static String matchString(String input){
String regex = "(?:\\w*: )((?:\\d+\\.?)*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
}
输出:
0.0.21.201
7.202.10.100
0.33.189.142
19
50715
在扫描仪循环中,您可以处理该行。我无法理解你的问题,如果你只想得到数字,或者用字符串[name]=“[number]”替换每一行。如果是这种情况,您只需在扫描器的循环中篡改一点,并删除正则表达式中的非捕获组(这意味着删除\w之前的“?:”),以便它也捕获单词
希望有帮助 试一试,它将包含多行的字符串作为输入,并逐行输出数字(从外观上看是IP和端口)
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
因为问题本身就是正则表达式,所以我在阅读过程中没有花太多精力,所以我只使用了扫描仪,没有费心读/写文本文件,我想你已经得到了这一部分
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainRegex {
/**
* @param args
*/
public static void main(String[] args) {
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String output = matchString(line);
System.out.println(output);
}
scanner.close();
}
public static String matchString(String input){
String regex = "(?:\\w*: )((?:\\d+\\.?)*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
}
输出:
0.0.21.201
7.202.10.100
0.33.189.142
19
50715
在扫描仪循环中,您可以处理该行。我无法理解你的问题,如果你只想得到数字,或者用字符串[name]=“[number]”替换每一行。如果是这种情况,您只需在扫描器的循环中篡改一点,并删除正则表达式中的非捕获组(这意味着删除\w之前的“?:”),以便它也捕获单词
希望有帮助 试一试,它将包含多行的字符串作为输入,并逐行输出数字(从外观上看是IP和端口)
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);
因为问题本身就是正则表达式,所以我在阅读过程中没有花太多精力,所以我只使用了扫描仪,没有费心读/写文本文件,我想你已经得到了这一部分
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainRegex {
/**
* @param args
*/
public static void main(String[] args) {
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String output = matchString(line);
System.out.println(output);
}
scanner.close();
}
public static String matchString(String input){
String regex = "(?:\\w*: )((?:\\d+\\.?)*)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String matched = m.group(1);
return matched;
}
return null;
}
}
输出:
0.0.21.201
7.202.10.100
0.33.189.142
19
50715
在扫描仪循环中,您可以处理该行。我无法理解你的问题,如果你只想得到数字,或者用字符串[name]=“[number]”替换每一行。如果是这种情况,您只需在扫描器的循环中篡改一点,并删除正则表达式中的非捕获组(这意味着删除\w之前的“?:”),以便它也捕获单词
希望有帮助 你能展示你尝试过的东西吗?这个问题似乎偏离主题,因为它在发布之前没有显示任何研究成果。你能展示你尝试过的东西吗?这个问题似乎偏离主题,因为它在发布之前没有显示任何研究成果。你能展示你尝试过的东西吗?这个问题似乎偏离主题,因为它没有显示任何研究成果发帖前的努力。你能展示一下你尝试过什么吗?这个问题似乎脱离主题,因为它在发帖前没有显示任何研究努力。顺便说一句,使用这种方式你不需要事先知道单词本身,比如“srcadr”之类。顺便说一句,使用这种方式你不需要知道单词本身,比如“srcadr”之类,事先。顺便说一下,用这种方法你不需要知道
String input = "SrcAddr: 0.0.21.201\n"+
"DstAddr: 7.202.10.100\n"+
"NextHop: 0.33.189.142\n"+
"InputIf: 19\n"+
"OutputIf: 50715";
String SrcAddr = input.replaceAll("(?s).*SrcAddr:\\s([\\d\\.]+)\\s.*", "$1");
String DstAddr = input.replaceAll("(?s).*DstAddr:\\s([\\d\\.]+)\\s.*", "$1");
System.out.println(SrcAddr);
System.out.println(DstAddr);