正则表达式:在两个词之间捕获一个词 [英] Regex: Capture a word between two words

查看:120
本文介绍了正则表达式:在两个词之间捕获一个词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说这些是我的投入:

Let's say these are my inputs:

类型数据库xyz {abc}

type Database xyz{ abc }

类型数据库{abc}

type Database { abc }

我想在两种情况下都捕捉到这一点

I want to capture just this in both the cases

数据库

模式是:

类型" +任意数量的空格+我想要的+任意数量的空格+任何字符

"type" + any number of spaces + what i want + any number of spaces + any characters

到目前为止,我已经知道了,但是我不确定如何匹配任何字符. (?<=type)\s+(.*)(?=)

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));
}

找到单词并打印

Type: Database

这篇关于正则表达式:在两个词之间捕获一个词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆