Regex: Capture a word between two words

别等时光非礼了梦想. 提交于 2020-06-29 03:41:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!