Tuco Tuco - 11 months ago 52
Java Question

Regular Expression to obtain inner text

I want to obtain the highlighted part from the following text:

Linux version 3.10.49-g5796782 (d00903065@ubuntu)

I've been trying with Pattern and Matcher from the Java library but haven't been able to succeed. Thanks!

EDIT

Here's my code, I've tried some of the answers but for some reaon it won't have any matches:


String s = "Linux version 3.10.49-g5796782 (d00903065@ubuntu)";
Pattern pattern = Pattern.compile("'^\\D+([-.\\w]+)'");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
s = matcher.group(1);
} else {
s = "no match";
}

Answer Source

Have you tried something like this?

(?<=version\s)(\S+)

Here's a working example.