简体   繁体   English

正则表达式:Java:2个空格之间的匹配词

[英]regex: Java: match word between 2 spaces

How can I extract the "id" from the following string using regex. 如何使用正则表达式从以下字符串中提取“ id”。

string = 11,"col=""book"" id=""title"" length=""10""

I need to be able to extract the "id" header along with the value "title". 我需要能够提取“ id”标头以及值“ title”。

outcome: id=""title"" 结果:id =“” title“”

I am trying to the use split function with a regex to extract the identifier from the string. 我正在尝试使用带有正则表达式的split函数从字符串中提取标识符。

Try this: 尝试这个:

String result = "col=\"book\" id=\"title\" length=\"10\"";
String pattern = ".*(id\\s*=\\s*\"[^\"]*\").*";
System.out.println(result.replaceAll(pattern,"$1"));

Cheers! 干杯!

Use Pattern and Matcher classes to find what you are looking for. 使用Pattern和Matcher类查找所需内容。 Try to find these regex \\\\bid=[^ ]* . 尝试找到这些正则表达式\\\\bid=[^ ]*

String data = "string = 11,\"col=\"\"book\"\" id=\"\"title\"\" length=\"\"10\"\"";
Matcher m = Pattern.compile("\\bid=[^ ]*").matcher(data);
if (m.find())
    System.out.println(m.group());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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