简体   繁体   中英

regex: Java: match word between 2 spaces

How can I extract the "id" from the following string using regex.

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

I need to be able to extract the "id" header along with the value "title".

outcome: id=""title""

I am trying to the use split function with a regex to extract the identifier from the string.

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. Try to find these regex \\\\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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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