问题
Let's say these are my inputs:
type Database xyz{ abc }
type Database { abc }
I want to capture just this in both the cases
Database
The pattern is:
"type" + any number of spaces + what i want + any number of spaces + any characters
I've this so far but I'm not sure how to match any character in look ahead.
(?<=type)\s+(.*)(?=)
回答1:
I'm sure you don't need a lookbehind, because just matching and capturing the second word should work:
String input = "type Database xyz{ abc }";
Pattern pattern = Pattern.compile("type\\s+(.*?)\\s+.*");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
That finds the word and prints
Type: Database
来源:https://stackoverflow.com/questions/60405337/regex-capture-a-word-between-two-words