gpt4 book ai didi

java - 如何使用正则表达式解析属性

转载 作者:行者123 更新时间:2023-11-30 11:40:01 25 4
gpt4 key购买 nike

<分区>

我有一个属性文件 (myfile.prop),其属性如下:

name=sara
address=${this.address}
this.address=Bangalore
url=${abc}/${cde}
abc=lolkata
cde=florida
nested=${${this.work}.file}
this.work=engg
engg.file=mywork

我需要解析这些属性,以便每次都能获得解析后的值。

它的代码是:

static String resolve(String str, Properties props, boolean isResolved)
{
if (props == null || str == null)
{
return str;
}

String expandedString = str;
String macroPattern = "(\\$\\{)([a-z.\\-A-Z0-9_]*)(\\})";
Pattern pattern = Pattern.compile(macroPattern);
Matcher m = null;
boolean pendingItems = true;

do
{
m = pattern.matcher(expandedString);
pendingItems = false;
String formerStr = expandedString;

while (m.find())
{
pendingItems = true;
String propVal = (String)props.get(m.group(2));
if (propVal == null)
{
propVal = m.group(0);
}
expandedString = expandedString.replace(m.group(0), propVal);

if (!fullyResolve)
{
return expandedString;
}
}
m.reset();

/* if the string did not change at all, it is a sign that this
* string cannot be resolved, just return */
if (expandedString.equals(formerStr))
{
return expandedString;
}
}
while (pendingItems);

return expandedString.trim();
}

但它所做的只是返回值....有人能告诉我它有什么问题吗?

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