У меня есть строка текста, подобная этой:
This is a[WAIT] test.
Что я хочу сделать, это найти в строке подстроку, которая начинается с [и заканчивается]. Каждую найденную строку я хочу добавить.в ArrayList и замените подстроку в исходной строке на ^
Вот мое регулярное выражение:
String regex_script = "/^\\[\\]$/"; //Match a string which starts with the character [ ending in the character ]
Вот что у меня есть:
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regex_script); // Create a pattern to match
Matcher m = p.matcher(line); // Create a matcher with an input string
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "^");
result = m.find();
}
m.appendTail(sb); // Add the last segment of input to the new String
какхотел бы я сделать это?Спасибо