请帮助我如何使用java正则表达式来描述方案具体如何识别要匹配的特定模式:
我的输入字符串可能如下所示:
something + {SomeProductSet1}.count + {SomeOtherProductSet2}.amount >
{SomeProductSet3}.count + {SomeUSOC4}.amount
我需要使用类似的内容替换{}
中的所有内容
something + [abc].count+[xyz].count+[something].count + [xom].amount+
[ytkd].amount > [d].count
基本上,介于两者之间" {..}"它等同于我后来使用" [..]"列出的东西。 我有[...]的东西列表但是,我怎样才能识别" ' {...}' part它是可变长度和可变字符集。
如果使用正则表达式,我将用作模式吗?
谢谢!!非常感谢。
答案 0 :(得分:0)
在Java中,有很多方法可以编写代码来获取括号(或任何两个特定字符)之间的内容,但是你想要使用正则表达式,这对于这种事情来说可能相当慢特别是在处理提供的字符串中的多个括号对实例时。
您想要收集 Curly Brackets 之间所包含内容的基本代码可能是这样的:
String myString = "something + {SomeProductSet1}.count + {SomeOtherProductSet2}.amount > \n" +
" {SomeProductSet3}.count + {SomeUSOC4}.amount";
Matcher match = Pattern.compile("\\{([^}]+)\\}").matcher(myString);
while(match.find()) {
System.out.println(match.group(1));
}
上述正则表达式的含义:
\\{
打开卷曲括号字符{
(
开始匹配组[
其中一个字符^
不是以下字符}
使用之前的^
,这意味着"除了关闭之外的每个字符
Curly Bracket }
" +
来自[]
集)
停止匹配组\\}
文字结束曲线括号}
如果是我,我会创建一个方法来存放这段代码,以便它可以用于其他括号类型,如:圆括号(),方括号[],卷曲括号{}(如代码所示) ,Chevron Brackets<>,或甚至在任何两个提供的字符之间,如:/.../或%...%或甚至A ... A。请参阅下面的示例方法,它演示了这一点
在上面的示例代码中,它将位于 while 循环中,您将处理每组括号中找到的每个子字符串。您当然需要一种机制来确定检测到哪个子字符串将被替换为可能是多维数组的任何字符串,或者甚至可能是一个自定义对话框,它会在每个括号之间显示找到的子字符串,并允许用户从带有全部选项复选框的组合框。当然,这里有几个选项可用于处理每组括号中每个找到的子串的方式和内容。
这是一个方法示例,演示了我们在此讨论的内容。评论很好:
public String replaceBetween(String inputString, String openChar,
String closeChar, String[][] replacements) {
// If the supplied input String contains nothing
// then return a Null String ("").
if (inputString.isEmpty()) { return ""; }
// Declare a string to hold the input string, this way
// we can freely manipulate it without jeopordizing the
// original input string.
String inString = inputString;
// Set the escape character (\) for RegEx special Characters
// for both the openChar and closeChar parameters in case
// a character in each was supplied that is a special RegEx
// character. We'll use RegEx to do this.
Pattern regExChars = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
String opnChar = regExChars.matcher(openChar).replaceAll("\\\\$0");
String clsChar = regExChars.matcher(closeChar).replaceAll("\\\\$0");
// Create our Pattern to find the items contained between
// the characters tht was supplied in the openChar and
// closeChar parameters.
Matcher m = Pattern.compile(opnChar + "([^" + closeChar + "]+)" + clsChar).matcher(inString);
// Iterate through the located items...
while(m.find()) {
String found = m.group(1);
// Lets see if the found item is contained within
// our supplied 2D replacement items Array...
for (int i = 0; i < replacements.length; i++) {
// Is an item found in the array?
if (replacements[i][0].equals(found)) {
// Yup... so lets replace the found item in our
// input string with the related element in our
// replacement array.
inString = inString.replace(openChar + found + closeChar, replacements[i][1]);
}
}
}
// Return the modified input string.
return inString;
}
要使用此方法,您可以这样做:
// Our 2D replacement array. In the first column we place
// the substrings we would like to find within our input
// string and in the second column we place what we want
// to replace the item in the first column with if it's
// found.
String[][] replacements = {{"SomeProductSet1", "[abc]"},
{"SomeOtherProductSet2", "[xyz]"},
{"SomeProductSet3", "[xom]"},
{"SomeUSOC4", "[ytkd]"}};
// The string we want to modify (the input string):
String example = "something + {SomeProductSet1}.count + {SomeOtherProductSet2}.amount > \n" +
" {SomeProductSet3}.count + {SomeUSOC4}.amount";
// Lets make the call and place the returned result
// into a new variable...
String newString = replaceBetween(example, "{", "}", replacements);
// Display the new string variable contents
// in Console.
System.out.println(newString);
控制台应显示:
something + [abc].count + [xyz].amount >
[xom].count + [ytkd].amount
请注意它是如何取代Curly Brackets的?这似乎是您的要求之一,但可以轻松修改,只需替换括号之间的子串。也许您可以修改此方法(如果您愿意)可选地执行此操作,并作为另一个添加的可选功能....允许它忽略字母大小写。