gpt4 book ai didi

java - 如何获得所有具有相同模式的字符串

转载 作者:行者123 更新时间:2023-12-02 08:52:05 25 4
gpt4 key购买 nike

我有一个具有此结构的 xml 文件

<expression>[Customer ].[Sales ].[L_MOIS]</expression><expression>cast_varchar([Customer ].[Sales ].[L_MOIS_ANNEE])
+ ' ' +
cast_varchar([Customer ].[Sales ].[C_ANNEE])</expression></dataItem></selection><detailFilters><detailFilter><filterExpression>[Customer ].[Sales ].[DT_JOUR] <= getdate()</filterExpression></detailFilter></detailFilters></query><query name="RSmag"><source><model /></source><selection><dataItem aggregate="none" name="Code magasin"><expression>[Customer statistics].[Stores].[C_MAGASIN]</expression></dataItem><dataItem aggregate="none" name="Libellé magasin" sort="ascending"><expression>[Customer statistics].[Stores].[L_MAGASIN]</expression></dataItem></selection><detailFilters><detailFilter><filterExpression>[Customer statistics].[Stores].[C_DEPOT] <>'500'</filterExpression></detailFilter><detailFilter><filterExpression>[Customer statistics].[Stores].[C_MAGASIN] not in ('005120';'005130';'005140')</filterExpression></detailFilter></detailFilters>
</query><query name="CAdept_avec_metier_cumul"><source><model /></source><selection><dataItem aggregate="none" name="Cod Metier" rollupAggregate="none"><expression>[Customer ].[Articles].[COD_DPTG]</expression></dataItem><dataItem name="Nombre de tickets" rollupAggregate="total">
<expression>count(distinct [Customer ].[Sales ].[ID_TICKET])</expression></dataItem><dataItem name="Nombre de tickets non affecté" rollupAggregate="total"><expression>count(distinct
(case
when [Customer ].[Sales ].[C_AFFECTATION] <> 1
then [Customer ].[Sales ].[ID_TICKET]
else null
end)
)</expression>

我想提取选项卡的所有名称,结果我应该有:[客户].[销售].[C_ANNEE][客户].[销售].[DT_JOUR]

但现在我得到的是:

客户

销售

C_ANNEE

File f = new File("");  
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(f), "UTF-8"));
String str;
while ((str = in.readLine()) != null) {

Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(str);
while (m.find()) {
listres.add(m.group(1));

}
}

最佳答案

将问题分成两个独立的部分:

1)使用合适的XML解析器解析XML数据,以提取我们想要的文本。

2)对于提取的文本字段,使用正则表达式提取所需的子字符串。

以下示例使用 SAX 解析器(顺便说一下,我使用的是 Java 13)。

假设我们有一个包含以下 XML 的文件:

<root>
<query name="RSmag">
<source>
<model />
</source>
<selection>
<dataItem aggregate="none" name="Code magasin">
<expression>
[Customer ].[Sales ].[L_MOIS]
</expression>
<expression>
cast_varchar([Customer ].[Sales ].[L_MOIS_ANNEE]) + ' ' + cast_varchar([Customer ].[Sales ].[C_ANNEE])
</expression>
</dataItem>
</selection>
<detailFilters>
<detailFilter>
<filterExpression>
[Customer ].[Sales ].[DT_JOUR] &lt;= getdate()
</filterExpression>
</detailFilter>
</detailFilters>
</query>
<query name="RSmag">
<source>
<model />
</source>
<selection>
<dataItem aggregate="none" name="Code magasin">
<expression>
[Customer statistics].[Stores].[C_MAGASIN]
</expression>
</dataItem>
<dataItem aggregate="none" name="Libellé magasin" sort="ascending">
<expression>
[Customer statistics].[Stores].[L_MAGASIN]
</expression>
</dataItem>
</selection>
<detailFilters>
<detailFilter>
<filterExpression>
[Customer statistics].[Stores].[C_DEPOT] &lt;&gt; '500'
</filterExpression>
</detailFilter>
<detailFilter>
<filterExpression>
[Customer statistics].[Stores].[C_MAGASIN]
not in ('005120';'005130';'005140')
</filterExpression>
</detailFilter>
</detailFilters>
</query>
<query name="CAdept_avec_metier_cumul">
<source>
<model />
</source>
<selection>
<dataItem aggregate="none" name="Cod Metier" rollupAggregate="none">
<expression>
[Customer ].[Articles].[COD_DPTG]
</expression>
</dataItem>
<dataItem name="Nombre de tickets" rollupAggregate="total">
<expression>
count(distinct [Customer ].[Sales ].[ID_TICKET])
</expression>
</dataItem>
<dataItem name="Nombre de tickets non affecté" rollupAggregate="total">
<expression>count(distinct
(case
when [Customer ].[Sales ].[C_AFFECTATION] &lt;&gt; 1
then [Customer ].[Sales ].[ID_TICKET]
else null
end)
)
</expression>
</dataItem>
</selection>
</query>
</root>

注意以下几点:

a) 我根据问题的示例数据做出了创建有效 XML 文档的有根据的猜测。

b) 我逃脱了 <>文本中的符号,通过使用 &lt;&gt; .

第 1 步 - 解析数据

此解决方案uses SAX对于解析 - 有很多替代方案。

以下命令将读取输入文件的每一行,并丢弃任何不属于 <expression> 的标签。或<filterExpression>标签。该设置可以根据需要进行调整( watchedElements )。

代码收集每个标签内的文本,并通过删除换行符和额外的空格来清理它。

这为我们提供了一组 10 个文本字符串,如下所示:

[Customer ].[Sales ].[L_MOIS]
cast_varchar([Customer ].[Sales ].[L_MOIS_ANNEE]) + ' ' + cast_varchar([Customer ].[Sales ].[C_ANNEE])
[Customer ].[Sales ].[DT_JOUR] <= getdate()
[Customer statistics].[Stores].[C_MAGASIN]
[Customer statistics].[Stores].[L_MAGASIN]
[Customer statistics].[Stores].[C_DEPOT] <> '500'
[Customer statistics].[Stores].[C_MAGASIN] not in ('005120';'005130';'005140')
[Customer ].[Articles].[COD_DPTG]
count(distinct [Customer ].[Sales ].[ID_TICKET])
count(distinct (case when [Customer ].[Sales ].[C_AFFECTATION] <> 1 then [Customer ].[Sales ].[ID_TICKET] else null end) )

第 2 步 - 应用正则表达式

对于每个字符串,我们使用正则表达式来查找我们想要的数据:

\[.*?\](\.\[.*?\])*

这会搜索开头的“[”,一直到下一个“]”,并重复此操作以查找零个或多个以句点分隔的后续“[”和“]”字符串。

为了处理不需要的子匹配,我们只保留组零:

Matcher m = pattern.matcher(text);
while (m.find()) {
System.out.println("*** Matches found : " + m.group(0));
}

这为我们提供了以下 12 个结果:

[Customer ].[Sales ].[L_MOIS]
[Customer ].[Sales ].[L_MOIS_ANNEE]
[Customer ].[Sales ].[C_ANNEE]
[Customer ].[Sales ].[DT_JOUR]
[Customer statistics].[Stores].[C_MAGASIN]
[Customer statistics].[Stores].[L_MAGASIN]
[Customer statistics].[Stores].[C_DEPOT]
[Customer statistics].[Stores].[C_MAGASIN]
[Customer ].[Articles].[COD_DPTG]
[Customer ].[Sales ].[ID_TICKET]
[Customer ].[Sales ].[C_AFFECTATION]
[Customer ].[Sales ].[ID_TICKET]

完整解决方案

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class ParseFromFileUsingSax {

// Looks for an opening "[" followed by a closing "]" with an
// optional "." to string items together into one group.
Pattern pattern = Pattern.compile("\\[.*?\\](\\.\\[.*?\\])*");

public void parseUsingSax() {
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

// the tags we will inspect (all others will be skipped):
Set<String> watchedElements = new HashSet();
watchedElements.add("expression");
watchedElements.add("filterExpression");

DefaultHandler handler = new DefaultHandler() {

private boolean inElement = false;
private StringBuilder stringBuilder;

@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (watchedElements.contains(name)) {
inElement = true;
stringBuilder = new StringBuilder();
}
}

@Override
public void characters(char[] buffer, int start, int length) throws SAXException {
if (inElement) {
stringBuilder.append(buffer, start, length);
}
}

@Override
public void endElement(String uri, String localName,
String name) throws SAXException {
if (watchedElements.contains(name)) {
inElement = false;
String extractedText = formatString(stringBuilder.toString());
System.out.println();
System.out.println("Extracted XML text : " + extractedText);
printMatches(extractedText);
}
}

};

saxParser.parse("C:/tmp/query_data.xml", handler);

} catch (Exception e) {
System.err.print(e);
}

}

private String formatString(String text) {
text = text.replaceAll("\\r\\n|\\r|\\n", " "); // remove newlines
text = text.replaceAll(" *", " "); // collapse multiple spaces
return text.trim(); // remove leading/trailing whitespace
}

private void printMatches(String text) {
Matcher m = pattern.matcher(text);
while (m.find()) {
System.out.println("*** Matches found : " + m.group(0));
}
}

}

关于java - 如何获得所有具有相同模式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60710140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com