Java正则表达式提取两个单词之间的字符串
我有一根像这样的线Java正则表达式提取两个单词之间的字符串,java,regex,Java,Regex,我有一根像这样的线 <br/><description>Using a combination of remote probes, (TCP/IP, SMB, HTTP, NTP, SNMP, etc...) it is possible to guess the name of the remote operating system in use, and sometimes its version.</description><br/><
<br/><description>Using a combination of remote probes, (TCP/IP, SMB, HTTP, NTP, SNMP, etc...) it is possible to guess the name of the remote operating system in use, and sometimes its version.</description><br/><fname>os_fingerprint.nasl</fname><br/><plugin_modification_date>2012/12/01</plugin_modification_date><br/><plugin_name>OS Identification</plugin_name><br/><plugin_publication_date>2003/12/09</plugin_publication_date><br/><plugin_type>combined</plugin_type><br/><risk_factor>None</risk_factor><br/><solution>n/a</solution><br/><synopsis>It is possible to guess the remote operating system.</synopsis><br/><plugin_output><br/>Remote operating system : Microsoft Windows Server 2008 R2 Enterprise Service Pack 1<br/>Confidence Level : 99<br/>Method : MSRPC<br/><br/> <br/>The remote host is running Microsoft Windows Server 2008 R2 Enterprise Service Pack 1</plugin_output><br/>
使用远程探测组合(TCP/IP、SMB、HTTP、NTP、SNMP等),可以猜测正在使用的远程操作系统的名称,有时它的版本。
os_fingerprint.nasl
2012/12/01
os Identification
2003/12/09
组合
无
n/a
可以猜测远程操作系统。
远程操作系统:Microsoft Windows Server 2008 R2 Enterprise Service Pack 1
置信度:99
方法:MSRPC
远程主机正在运行Microsoft Windows Server 2008 R2企业服务包1
我想提取“远程操作系统:”并获取“Microsoft Windows Server 2008 R2企业服务包1”
远程操作系统:Microsoft Windows Server 2008 R2企业服务包1
所以我用
Pattern pattern = Pattern.compile("(?<=\\bRemote operating system :\\b).*?(?=\\b<br/>\\b)");
Pattern Pattern=Pattern.compile((?在正则表达式中:
之后和\\b
之前没有空格
试着这样做:
Pattern.compile("(?<=\\bRemote operating system : \\b).*?(?=\\b<br/>\\b)");
// ^additional space
Pattern.compile(“(?)?
请注意,通常不建议对标记语言使用正则表达式。
但是,在这里,您对特定的文本字符串使用正则表达式,而该字符串恰好位于标记内部,所以我想这是可以的。尝试以下模式:“*”远程操作系统:(.*)
“
publicstaticvoidmain(字符串[]args)引发异常{
String s=“
使用远程探测组合(TCP/IP、SMB、HTTP、NTP、SNMP等…)可以猜测正在使用的远程操作系统的名称,有时还可以猜测其版本。
os_fingerprint.nasl
2012/12/01
os Identification
2003/12/09
组合
无
n/a
可以猜测远程操作系统。
远程操作系统:Microsoft Windows Server 2008 R2企业服务包1
置信度:99
方法:MSRPC
远程主机正在运行Microsoft Windows Server 2008 R2企业服务包1
;
Pattern=Pattern.compile(“.*远程操作系统:(.*?)
”);
匹配器m=模式匹配器;
if(m.find()){
系统输出println(m.group(1));
}
else System.out.println(“未找到”);
}
尝试下一步:
if (str.matches("^.*Remote operating system : ([^<]*).*$")) {
System.out.println(
str.replaceAll("^.*Remote operating system : ([^<]*).*$", "$1")
);
}
if(str.matches(“^.*”远程操作系统:([^
String test =
"<br/><description>Using a combination of remote probes, " +
"(TCP/IP, SMB, HTTP, NTP, SNMP, etc...) it is possible to guess " +
"the name of the remote operating system in use, and sometimes " +
"its version.</description><br/><fname>os_fingerprint.nasl</fname>" +
"<br/><plugin_modification_date>2012/12/01</plugin_modification_date>" +
"<br/><plugin_name>OS Identification</plugin_name><br/>" +
"<plugin_publication_date>2003/12/09</plugin_publication_date><br/>" +
"<plugin_type>combined</plugin_type><br/><risk_factor>None</risk_factor>" +
"<br/><solution>n/a</solution><br/><synopsis>It is possible to guess the " +
"remote operating system.</synopsis><br/><plugin_output><br/>Remote operating " +
"system : Microsoft Windows Server 2008 R2 Enterprise Service Pack 1<br/>" +
"Confidence Level : 99<br/>Method : MSRPC<br/><br/> <br/>The remote host is " +
"running Microsoft Windows Server 2008 R2 Enterprise Service Pack 1" +
"</plugin_output><br/>";
Pattern pattern = Pattern.compile("Remote\\soperating\\ssystem\\s:\\s(.+?)\\<br/>");
Matcher matcher = pattern.matcher(test);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
Microsoft Windows Server 2008 R2 Enterprise Service Pack 1
public static void main(String[] args) throws Exception {
String s = "<br/><description>Using a combination of remote probes, (TCP/IP, SMB, HTTP, NTP, SNMP, etc...) it is possible to guess the name of the remote operating system in use, and sometimes its version.</description><br/><fname>os_fingerprint.nasl</fname><br/><plugin_modification_date>2012/12/01</plugin_modification_date><br/><plugin_name>OS Identification</plugin_name><br/><plugin_publication_date>2003/12/09</plugin_publication_date><br/><plugin_type>combined</plugin_type><br/><risk_factor>None</risk_factor><br/><solution>n/a</solution><br/><synopsis>It is possible to guess the remote operating system.</synopsis><br/><plugin_output><br/>Remote operating system : Microsoft Windows Server 2008 R2 Enterprise Service Pack 1<br/>Confidence Level : 99<br/>Method : MSRPC<br/><br/> <br/>The remote host is running Microsoft Windows Server 2008 R2 Enterprise Service Pack 1</plugin_output><br/>";
Pattern pattern = Pattern.compile(".*Remote operating system : (.*?)<br/>");
Matcher m = pattern.matcher(s);
if (m.find()) {
System.out.println(m.group(1));
}
else System.out.println("Not found");
}
if (str.matches("^.*Remote operating system : ([^<]*).*$")) {
System.out.println(
str.replaceAll("^.*Remote operating system : ([^<]*).*$", "$1")
);
}