简体   繁体   English

正则表达式-第一次比赛后获得第二个单词

[英]Regex - get second word after first match

I'm trying to parse a simple DDL statement. 我正在尝试解析一个简单的DDL语句。 First I'm trying to pull the table name out. 首先,我试图拉出表名。 The syntax will be something like 'CREATE TABLE DB_NAME.TABLE_NAME' 语法类似于“ CREATE TABLE DB_NAME.TABLE_NAME”

So far I've got this: 到目前为止,我已经知道了:

String line = "CREATE TABLE DB_NAME.T_NAME";
String pattern = ".*?\\bTABLE\\s+(\\w+)\\b.*";
System.out.println(line.replaceFirst(pattern, "$1"));

That gives me back "DB_NAME". 那回给我“ DB_NAME”。 How can I get it to give me back "T_NAME"? 如何获取还给我的“ T_NAME”?

I tried following the update in this answer , but I couldn't get it to work, probably due to my very limited regex skills. 我尝试按照此答案中的更新进行操作,但由于我的正则表达式技能非常有限,因此无法正常工作。

What about sth like this: 怎么样呢?

.*?\\bTABLE\\s+\\w+\\.(\\w+)\\b.*

Demo 演示版

It first matches the TABLE keyword with .*?\\\\bTABLE\\\\s+ . 它首先将TABLE关键字与.*?\\\\bTABLE\\\\s+匹配。 Then it matches DB_NAME. 然后,它匹配DB_NAME. with \\\\w+\\\\. \\\\w+\\\\. . Finally it matches and captures T_NAME with (\\\\w+) 最后,它以(\\\\w+)匹配并捕获T_NAME

Here's a small piece of code that will do (using named capturing groups): 这是一小段将要执行的代码(使用命名的捕获组):

String line = "CREATE TABLE DB_NAME.T_NAME";
Pattern pattern = Pattern.compile("CREATE TABLE (?<database>\\w+)\\.(?<table>\\w+)");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
    String database = matcher.group("database"); // DB_NAME
    String table = matcher.group("table"); // T_NAME
}

You may extract all the string after the TABLE into a group and then split with comma to get individual values: 您可以将TABLE之后的所有字符串提取到一个组中,然后用逗号分割以获取单个值:

String line = "CREATE TABLE DB_NAME.T_NAME";
String pattern = "\\bTABLE\\s+(\\w+(?:\\.\\w+)*)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(line);
if (m.find()){
    System.out.println(Arrays.toString(m.group(1).split("\\."))); 
    // => [DB_NAME, T_NAME]
} 

See the Java demo . 请参阅Java演示

If you are sure of the incoming format of the string, you might even use 如果您确定字符串的传入格式,甚至可以使用

"\\bTABLE\\s+(\\S+)"

See another Java demo . 参见另一个Java演示

While \\w+(?:\\.\\w+)* matches 1+ word chars followed with 0+ repetitions of . 虽然\\w+(?:\\.\\w+)*匹配1个单词字符,后跟0个重复. and 1+ word chars, \\S+ plainly matches 1+ non-whitespace chars. 和1+个单词字符, \\S+明确匹配1+个非空白字符。

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

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