我有以下格式的字符串:
[string1][string2][string3][string4][string5][string6]
其中string1
的第一个字符串将始终为Year
,并且格式为YYYY
,因此示例示例为:
[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
现在在某些情况下,string1
采用此格式2010^K2011^K2012^K2013^K2014^K2015^K2016^K2017
,其中每年以^K
分隔符分隔。示例示例:
[2010^K2011^K2012^K2013^K2014^K2015^K2016^K2017][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
现在每当我看到上述格式的字符串时,我必须解析年份字符串并拆分分隔符并提取每个单独的年份,然后按以下格式制作字符串列表:
[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2011][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2012][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2013][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2014][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2015][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2016][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2017][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
所以我可以想到以下情况:
这可能吗?我知道如何在一个特殊字符^
上拆分一个字符串,但是混淆是如何提取string1
并检查它是否有多年(如果是,则拆分)或者它只有一年它或它还有别的东西。
public static void main(String[] args) {
// String myString =
// "[2010^K2011^K2012^K2013^K2014^K2015^K2016^K2017][Toyota][ALL][Hatchback][998ccm 68HP 50KW]";
String myString = "[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]";
}
答案 0 :(得分:1)
我想出了这个方法:
public static String[] splitYears(String str) {
// get the year part of the string using a regex
Matcher m = Pattern.compile("^\\[([\\d^K]+)\\]").matcher(str);
if(m.find()) {
String yearPart = m.group(1);
// separate the other parts. The + 2 here is to account for the two square brackets in the yearPart
String otherParts = str.substring(yearPart.length() + 2);
// split by ^K
String[] years = yearPart.split("\\^K");
// Construct a new string for each year
String[] newYears = new String[years.length];
for (int i = 0; i < years.length; i++) {
newYears[i] = String.format("[%s]%s", years[i], otherParts);
}
return newYears;
} else {
return new String[] {str};
}
}
用法:
System.out.println(Arrays.toString(splitYears("[2010^K2011^K2012^K2013^K2014^K2015^K2016^K2017][Toyota][ALL][Hatchback][998ccm 68HP 50KW]")));
答案 1 :(得分:1)
现在我看到你的问题有一个公认的答案,我想告诉你我试图解决你的问题 - 你可能想尝试一下或者在这里找到一些有趣的东西(我在这里添加了一些解释)对代码的评论)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s1 = "[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]";
String s2 = "[2010^K2011^K2012^K2013^K2014][Toyota][ALL][Hatchback][998ccm 68HP 50KW]";
String[] oneYearString = transformString(s1);
String[] moreYearsString = transformString(s2);
for(String s : oneYearString) {
System.out.println(s);
}
System.out.println();
for(String s : moreYearsString) {
System.out.println(s);
}
}
public static String[] transformString(String source) {
Pattern pattern = Pattern.compile("^\\[([^]]+)(.*)"); // contents of first square bracket are in first matching group
// in first parenthesis (), the following part is in second matching group parenthesis
Matcher m = pattern.matcher(source); // matcher that compares source String with given pattern
String[] result;
while(m.find()) {
if(m.group(1).length() == 4) { //if there are 4 characters in first square bracket...
result = new String[] {source};
return result; // return String[] containing only one element
} else { // if there are more than 4 characters...
String[] splittedYears = m.group(1).split("\\^K"); // split content of first bracket on given delimiter
result = new String[splittedYears.length]; // here you're gonna store result Strings
for(int i = 0; i < result.length; i++) {
String entry = "[" + splittedYears[i] + m.group(2); // creating properly formatted string for each year
result[i] = entry; // adding properly prepared String to array
}
return result; // return String[] with results
}
}
return null;
}
}
执行时得到的输出:
[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2010][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2011][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2012][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2013][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
[2014][Toyota][ALL][Hatchback][998ccm 68HP 50KW]
(仅发布更改方法)
public static String[] transformString(String source) {
String firstBracket = source.substring(source.indexOf('[') + 1, source.indexOf(']'));
String[] result;
if(firstBracket.length() == 4) {
result = new String[] {source};
return result;
} else {
System.out.println(firstBracket);
String[] splittedYears = firstBracket.split("\\^K");
result = new String[splittedYears.length];
for(int i = 0; i < result.length; i++) {
String entry = "[" + splittedYears[i] + source.substring(source.indexOf(']'));
result[i] = entry;
}
return result;
}
}
答案 2 :(得分:1)
即使这样也可以
String str = "([0-9]{4})(.*?(?=\\d))?(?:.*?)(\\[.*)";
Pattern pattern = Pattern.compile(str);
String input = "[2010^K2011^K2012^K2013^K2014^K2015^K2016^K2017][Toyota][ALL][Hatchback][998ccm 68HP 50KW]";
Matcher matcher;
do
{
matcher = pattern.matcher(input);
if (matcher.find())
{
System.out.println("[" + matcher.group(1) + "]" + matcher.group(3));
input = input.replace(matcher.group(1) + matcher.group(2), "");
}
} while (matcher.group(2) != null);
答案 3 :(得分:-1)
java.lang.String有一些方法可以让这很容易。
你可能应该分裂为单独隔离“string1”的每个字符串,如Scary Wombat所建议的那样。类似的东西:
String[] stringArray = myString.split("]\\[|\\[|]");
注意:这将在[0]处创建一个空字符串。
为了防止这种情况,我会抢先删除第一个“[”with
myString.replaceFirst();
现在你有一个字符串数组,第一个元素是你的年份。
for(String year : stringArray.split("\\^K")
{
do things in here.
}