【问题标题】:REgular Expression using Java [duplicate]使用Java的正则表达式[重复]
【发布时间】:2014-05-13 17:52:53
【问题描述】:

我正在使用正则表达式来中断字符串,我正在尝试中断字符串,但在正则表达式中我缺少一些格式。谁能告诉我我哪里出错了。

String betweenstring="['Sheet 1$'].[DEPTNO] AS [DEPTNO]";
System.out.println("betweenstring: "+betweenstring);
Pattern pattern = Pattern.compile("\\w+[.]\\w+");
Matcher matchers=pattern.matcher(betweenstring);    
while(matchers.find())
{       
    String filtereddata=matchers.group(0);          
    System.out.println("filtereddata: "+filtereddata);
}

我需要这样打破:

['Sheet 1$']
[DEPTNO] AS [DEPTNO]

【问题讨论】:

  • 定义“断线”是什么意思
  • 我已更新请检查一次

标签: java regex


【解决方案1】:

鉴于您的具体输入,此正则表达式有效。

([\w\[\]' $]+)\.([\w\[\]' $]+)

捕获组 1 在句点之前,捕获组 2 之后。为 Java 字符串转义:

Pattern pattern = Pattern.compile("([\\w\\[\\]' $]+(\\.*[\\w\\[\\]' $]+)");

但是,如果这是您想要实现的目标,那么在文字点上拆分字符串会容易得多:

String[] pieces = between.split("\\.");
System.out.println(pieces[0]);    
System.out.println(pieces[1]);

输出:

['Sheet 1$']
[DEPTNO] AS [DEPTNO]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2023-03-11
    • 2012-07-09
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多