【发布时间】:2021-03-14 02:36:32
【问题描述】:
我有一个这些字符串的列表,格式如下:
9-11 w: wwtxmsjwjwlw separate to: 9-11 , w , wwtxmsjwjwlw
5-6 g: wggghg separate to 5-6 , g , wggghg
1-5 g: dxggdggb seperate to 1-5 , g , dxggdggb
5-13 f: lfgdplfffxffswck separate to 5-13 , f, lfgdplfffxffswck
6-7 z: zzzzgzzzz separate to 6-7 , z , zzzzgzzzz
这些只是示例,因此实际上还有更多,只是为了帮助您了解它的外观,左侧已给出,右侧也是我想要更改的内容,并且能够访问未来的值使用就像将三个值的每种类型存储在三个数组中一样,它们共享相同的索引...... 如何分隔每个部分并以某种方式保留值以便我可以访问它们? 这是我尝试过的:
int[] a1 = new int[10];
int[] a2 = new int[10];
int[] a3 = new int[10];
for(int i=0;i<a1.length;i++){
a1[i].split("^[0-9]-[0-9]");//take the format of whatever looks like "int-int"
a1[i].split("/d[a/z]:");//take the letter before the: - :"char:"
a1[i].split("[a-z]{1,}");// take the long text that has a minimum of 1 char "String"for example -zzzzgzzzz
}
如果有比我对数组的想法更有效的方法和正则表达式的不良性能并修复我的方法,请告诉我,因为它不起作用。 问题的目的是获取字母/字符的出现,基本上检查给定范围 int-int 的出现在它的字符串中是否为真然后 count++,它不一定是真的 - 没有任何反应。 - 但如果这有意义,我想检查字母是否出现在字符串的给定范围内 帮助改进问题也会有所帮助,谢谢
这里又是使用下面解决方案的代码,感谢@wp78de,我尝试对其进行更改,以便它使用 reader.nextLine() 来获取输入和数组中的位置-
public static Scanner reader= new Scanner(System.in);
public static int testString(String s, int c) {
String regex = "(\\d+)-(\\d+) (\\w): ([a-z]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
matcher.find();
//System.out.println("Full match: " + matcher.group(0));
Pattern p = Pattern.compile(matcher.group(3));
Matcher m = p.matcher(matcher.group(4));
long count = m.results().count();
if(count < Long.parseLong(matcher.group(1)) || count > Long.parseLong(matcher.group(2))) {
}
else {
c++;
}
return c;
}
public static void main (String[] args) throws java.lang.Exception
{
int count=0;
for(int i=0;i<1000;i++) {
String ss = reader.nextLine();
for (String s: ss.split("\n")) {
System.out.println(s);
testString(s,count);
System.out.println(s + " : "+ count);
}
}
System.out.println(count);
}
}
【问题讨论】:
-
你试过使用子串吗? Take a look at them.
-
那是javascript,java?
-
@rokk_crow 已编辑评论。现在就来看看吧。