简体   繁体   English

正则表达式:在两个单词之间捕获一个单词

[英]Regex: Capture a word between two words

Let's say these are my inputs:假设这些是我的输入:

type Database xyz{ abc }类型数据库xyz{abc}

type Database { abc }类型数据库{ 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+(.*)(?=)

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

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

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