在Java中,有很多方法可以編寫代碼來獲取括號內的內容(或者任何兩個特定的字符),但你想用一個正則表達式來討論,對於這種類型的特別是在處理提供的字符串中的多個括號對實例時。
要收拾包含波形括號之間的內容可以是這樣的基本代碼:
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));
}
什麼上述正則表達式的意思是:
\\{
打開花括號字符{
(
開始匹配組
[
這些字符
^
不是以下字符
}
與以前^
之一,這意味着
+
的多個其他人物之一從[]
集「除了關閉 花括號}
每個字符」
)
停止匹配組
\\}
literal閉合捲曲支架}
如果是我,我想創建以容納這個碼的方法,以便它可以被用於其它支架類型以及類似:括號(),方括號[],位於大括號{}(如如代碼所示),Chevron支架<>,或者甚至在任何兩個提供的字符之間:/.../或%...%或甚至A ... A。請參閱下面的示例方法來演示這一點。
在上面的示例代碼中,它將位於而循環中,您將處理在每組括號之間找到的每個子字符串。您當然需要一種機制來確定檢測到的哪個子字符串要替換爲可能是多維數組的任何字符串,或者甚至可以是自定義對話框,該對話框將在每個括號之間顯示找到的子字符串,並允許用戶從可能的一個組合框做所有選項複選框。當然,這裏有幾個選項可用來處理每組括號之間的每個找到的子串的處理方式和處理方法。
下面是一個方法示例,它演示了我們在這裏討論的內容。它有很好的註釋:
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
注意怎麼也代替大括號?這似乎是您的要求之一,但可以很容易地修改爲只替換括號內的子字符串。也許你可以修改這個方法(如果你喜歡的話)來做這個選擇,並且作爲又一個添加的可選功能....允許它忽略字母大小寫。
你如何配合替代品的投入?按位置? – shmosel