Java 按关键字子字符串
Java Substring by Keyword
我必须从一个字符串中创建单独的字符串。
例如给定字符串:
.*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*
我必须创建一个包含以下内容的 HashSet:
.*C.{0}A.{2}T.{0}T.*
.*A.{2}T.{0}T.{0}T.*
.*T.{0}T.{0}T.{2}T.*
.*T.{0}T.{2}T.{0}G.*
...
这些元素是通过从原始字符串中提取 4 个条目并从中创建一个较小的字符串而形成的。然后在原始字符串中移动一个条目并重复。
我该怎么做?
谢谢!
您想获取一个表示元素列表的字符串,并将其转换为一组重叠的较短元素列表。您可以通过使用 returns 列表中的元素然后滑动 window 选择要显示的元素集的方法来做到这一点:
private static final Pattern pattern = Pattern.compile("[ACGT]\.\{\d+\}");
public static List<String> extract(String input) {
Matcher matcher = pattern.matcher(input);
List<String> result = new ArrayList<String>();
while (matcher.find()) {
result.add(matcher.group(0));
}
return result;
}
public static Set<String> compose(List<String> elements, int window) {
Set<String> result = new HashSet<String>();
for (int i = 0; i <= elements.size() - window; i++) {
StringBuilder builder = new StringBuilder(".*");
for (int j = i; j < i + window; j++) {
builder.append(elements.get(j));
}
// This strips the final quantifier turning:
// .*C.{0}A.{2}T.{0}T.{0}
// into
// .*C.{0}A.{2}T.{0}T
builder.delete(builder.lastIndexOf("."), builder.length());
builder.append(".*");
result.add(builder.toString());
}
return result;
}
您可以通过以下方法查看:
public static void main(String[] args) {
String input = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}";
Set<String> result = compose(extract(input), 4);
// The result will contain
// ".*C.{0}A.{2}T.{0}T.*"
// etc
}
这是一个可能的解决方案:
public class Main {
public static void main(String[] args) {
String s = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*";
String[] array = s.split("}");
Set<String> result = new HashSet<String>();
for ( int i = 0 ; i < array.length-3 ; i++) {
String firstElement = array[i].startsWith(".*") ? array[i].substring(2) : array[i];
String lastElement = array[i+2]+"}"+array[i+3].substring(0,1)+".*" ;
String element = ".*"+firstElement+"}"+array[i+1]+"}"+lastElement;
result.add(element);
System.out.println(element);
}
//Your result are in the Set result
}
}
我必须从一个字符串中创建单独的字符串。
例如给定字符串:
.*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*
我必须创建一个包含以下内容的 HashSet:
.*C.{0}A.{2}T.{0}T.*
.*A.{2}T.{0}T.{0}T.*
.*T.{0}T.{0}T.{2}T.*
.*T.{0}T.{2}T.{0}G.*
...
这些元素是通过从原始字符串中提取 4 个条目并从中创建一个较小的字符串而形成的。然后在原始字符串中移动一个条目并重复。
我该怎么做?
谢谢!
您想获取一个表示元素列表的字符串,并将其转换为一组重叠的较短元素列表。您可以通过使用 returns 列表中的元素然后滑动 window 选择要显示的元素集的方法来做到这一点:
private static final Pattern pattern = Pattern.compile("[ACGT]\.\{\d+\}");
public static List<String> extract(String input) {
Matcher matcher = pattern.matcher(input);
List<String> result = new ArrayList<String>();
while (matcher.find()) {
result.add(matcher.group(0));
}
return result;
}
public static Set<String> compose(List<String> elements, int window) {
Set<String> result = new HashSet<String>();
for (int i = 0; i <= elements.size() - window; i++) {
StringBuilder builder = new StringBuilder(".*");
for (int j = i; j < i + window; j++) {
builder.append(elements.get(j));
}
// This strips the final quantifier turning:
// .*C.{0}A.{2}T.{0}T.{0}
// into
// .*C.{0}A.{2}T.{0}T
builder.delete(builder.lastIndexOf("."), builder.length());
builder.append(".*");
result.add(builder.toString());
}
return result;
}
您可以通过以下方法查看:
public static void main(String[] args) {
String input = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}";
Set<String> result = compose(extract(input), 4);
// The result will contain
// ".*C.{0}A.{2}T.{0}T.*"
// etc
}
这是一个可能的解决方案:
public class Main {
public static void main(String[] args) {
String s = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*";
String[] array = s.split("}");
Set<String> result = new HashSet<String>();
for ( int i = 0 ; i < array.length-3 ; i++) {
String firstElement = array[i].startsWith(".*") ? array[i].substring(2) : array[i];
String lastElement = array[i+2]+"}"+array[i+3].substring(0,1)+".*" ;
String element = ".*"+firstElement+"}"+array[i+1]+"}"+lastElement;
result.add(element);
System.out.println(element);
}
//Your result are in the Set result
}
}