Java字符串匹配表达式字符串数组
我正在使用Java,我需要你对如何为下面的任务编写更好的代码发表意见 我有以下字符串值Java字符串匹配表达式字符串数组,java,string,Java,String,我正在使用Java,我需要你对如何为下面的任务编写更好的代码发表意见 我有以下字符串值 String testStr = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(ABC) INCLUDES(ABC)" 我想操纵字符串,并想将所有INCLUDES语句组合成一个INCLUDES,结果应该类似于以下内容: INCLUDES(ABC,ABC, ABC) EXCLUDES(ABC, ABC) 我将使用以下类将初始字符串拆分为新字符串: 然
String testStr = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(ABC) INCLUDES(ABC)"
我想操纵字符串,并想将所有INCLUDES语句组合成一个INCLUDES,结果应该类似于以下内容:
INCLUDES(ABC,ABC, ABC) EXCLUDES(ABC, ABC)
我将使用以下类将初始字符串拆分为新字符串: 然后把它们排成一列 启动一个新字符串,然后使用标记器将圆括号内的部分分隔开(您可以将其设置为使用(和)作为分隔符),循环遍历数组并将它们连接到新字符串中
但要注意,任何错误放置的空间(如INCLUDES(abc))都会将其弄乱这似乎是一种合理的方法:
StringUtils.Split
方法拆分testStr
;使用“”或null作为标记地图
。我将其称为theMap
INCLUDES(ABC,ABC, ABC) EXCLUDES(ABC, ABC)
testStr
返回的数组后,使用theMap
中的键值并将关联列表中的元素追加到字符串中,构建一个新字符串
我为这个问题写了一段代码,但我不知道它是好是坏
- 根据您的格式,您可以使用“拆分testStr,输出如下:包含(ABC)
- 检查此字符串是否包含包含或排除
- 然后使用()
String testStr = "INCLUDES(ABC) EXCLUDES(C) EXCLUDES(ABC) INCLUDES(AC) INCLUDES(AB)";
String s[] = testStr.split(" ");
String INCLUDES = "INCLUDES( ";
String EXCLUDES = "EXCLUDES ( ";
for (int i = 0; i < s.length; i++) {
if (s[i].contains("INCLUDES")) {
INCLUDES += (s[i].substring(s[i].indexOf("(") + 1, s[i].indexOf(")"))) + " ";
}
else if (s[i].contains("EXCLUDES")) {
EXCLUDES += (s[i].substring(s[i].indexOf("(") + 1, s[i].indexOf(")"))) + " ";
}
}
INCLUDES = INCLUDES + ")";
EXCLUDES = EXCLUDES + ")";
System.out.println(INCLUDES);
System.out.println(EXCLUDES);
String testStr=“INCLUDES(ABC)EXCLUDES(C)EXCLUDES(ABC)INCLUDES(AC)INCLUDES(AB)”;
字符串s[]=testStr.split(“”);
字符串INCLUDES=“INCLUDES(”;
String EXCLUDES=“EXCLUDES(”;
对于(int i=0;i
我已经写下了下面的小实用程序结果
if text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
output = EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG & EFG & IJK)
下面是我的java代码,请看一看,如果有人能改进它,请随意
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sun.xml.internal.ws.util.StringUtils;
/**
* Created by IntelliJ IDEA.
* User: fkhan
* Date: Aug 31, 2012
* Time: 1:36:45 PM
* To change this template use File | Settings | File Templates.
*/
public class TestClass {
public static void main(String args[]) throws Exception {
//String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(EFG) INCLUDES(IJK)";
String text = "EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(BMG) INCLUDES(EFG) INCLUDES(IJK)";
List<String> matchedList = findMatchPhrase("INCLUDES", text);
String query = combinePhrase(text, "INCLUDES", matchedList);
System.out.println(query);
}
/**
* This method takes query combine and & multiple phrases
* @param expression
* @param keyword
* @param matchedItemList
* @return
*/
private static String combinePhrase(String expression, String keyword, List<String> matchedItemList) {
//if only one phrase found return value
if(matchedItemList.isEmpty() || matchedItemList.size() ==1){
return expression;
}
//do not remove first match
String matchedItem = null;
for (int index = 1; index < matchedItemList.size(); index++) {
matchedItem = matchedItemList.get(index);
//remove match items from string other then first match
expression = expression.replace(matchedItem, "");
}
StringBuffer textBuffer = new StringBuffer(expression);
//combine other matched strings in first matched item
StringBuffer combineStrBuf = new StringBuffer();
if (matchedItemList.size() > 1) {
for (int index = 1; index < matchedItemList.size(); index++) {
String str = matchedItemList.get(index);
combineStrBuf.append((parseValue(keyword, str)));
combineStrBuf.append(" & ");
}
combineStrBuf.delete(combineStrBuf.lastIndexOf(" & "), combineStrBuf.length());
}
// Inject created phrase into first phrase
//append in existing phrase
return injectInPhrase(keyword, textBuffer, combineStrBuf.toString());
}
/**
*
* @param keyword
* @param textBuffer
* @param injectStr
*/
private static String injectInPhrase(String keyword, StringBuffer textBuffer, String injectStr) {
Matcher matcher = getMatcher(textBuffer.toString());
while (matcher.find()) {
String subStr = matcher.group();
if (subStr.startsWith(keyword)) {
textBuffer.insert(matcher.end()-1, " & ".concat(injectStr));
break;
}
}
return textBuffer.toString();
}
/**
* @param expression
* @param keyword
* @return
*/
private static String parseValue(String keyword, String expression) {
String parsStr = "";
if (expression.indexOf(keyword) > -1) {
parsStr = expression.replace(keyword, "").replace("(", "").replace(")", "");
}
return parsStr;
}
/**
* This method creates matcher object
* and return for further processing
* @param expression
* @return
*/
private static Matcher getMatcher(String expression){
String patternString = "(\\w+)\\((.*?)\\)";
Pattern pattern = Pattern.compile(patternString);
return pattern.matcher(expression);
}
/**
* This method find find matched items by keyword
* and return as list
* @param keyword
* @param expression
* @return
*/
private static List<String> findMatchPhrase(String keyword, String expression) {
List<String> matchList = new ArrayList<String>(3);
keyword = StringUtils.capitalize(keyword);
Matcher matcher = getMatcher(expression);
while (matcher.find()) {
String subStr = matcher.group();
if (subStr.startsWith(keyword)) {
matchList.add(subStr);
}
}
return matchList;
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入com.sun.xml.internal.ws.util.StringUtils;
/**
*由IntelliJ IDEA创建。
*用户:fkhan
*日期:2012年8月31日
*时间:下午1:36:45
*要更改此模板,请使用文件|设置|文件模板。
*/
公共类TestClass{
公共静态void main(字符串args[])引发异常{
//String text=“包括(ABC)排除(ABC)排除(ABC)包括(EFG)包括(IJK)”;
String text=“排除(ABC)排除(ABC)包括(BMG)包括(EFG)包括(IJK)”;
列表匹配列表=FindMatchPhase(“包括”,文本);
字符串查询=组合数据库(文本,“包括”,匹配列表);
System.out.println(查询);
}
/**
*此方法使用查询组合和多个短语(&M)
*@param表达式
*@param关键字
*@param matchedItemList
*@返回
*/
私有静态字符串组合数据库(字符串表达式、字符串关键字、列表匹配编辑器列表){
//如果只找到一个短语返回值
if(matchedItemList.isEmpty()| | matchedItemList.size()==1){
返回表达式;
}
//不要删除第一个匹配项
字符串matchedItem=null;
对于(int index=1;index1){
对于(int index=1;index-1){
parstr=expression.replace(关键字“”).replace(“,”).replace(“),”);
}
返回