public static void main(String[] args) { String str = "马五↑(3)↓(1)↑(4)↓(2)↓(6)"; int up = getInt(str, "(?<=↑\\()\\d(?=\\))"); //上箭头 int down = getInt(str, "(?<=↓\\()\\d(?=\\))"); //下箭头 System.out.println(up); System.out.println(down); } public static int getInt(String str, String regex){ Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); String result = ""; while(m.find()){ result += m.group(); } return Integer.parseInt(result); }
String s1="",s2=""; Matcher m; m = Pattern.compile("↑\\((\\d*)\\)").matcher("马五↑(3)↓(1)↑(4)↓(2)↓(6)"); while(m.find()) { s1 += m.group(1); } m = Pattern.compile("↓\\((\\d*)\\)").matcher("马五↑(3)↓(1)↑(4)↓(2)↓(6)"); while(m.find()) { s2 += m.group(1); } System.out.println(s1); System.out.println(s2);
不用正则 String s = "马五↑(3)↓(1)↑(4)↓(2)↓(6)"; s = s.replaceAll("[(]|[)]","").replaceAll("[^\\d|↓|↑]",""); System.out.println(s); int i=0; List l1 = new ArrayList(); List l2 = new ArrayList(); int j; while((j = s.indexOf("↑",i))!=-1){ l1.add(s.charAt(j+1)); i=j+1; } System.out.println(l1); i = 0; while((j = s.indexOf("↓",i))!=-1){ l2.add(s.charAt(j+1)); i=j+1; } System.out.println(l2);
不好意思 开始就想替换来着 后面就没改了 index +1 改成+2就可以了 String s = "马五↑(3)↓(1)↑(4)↓(2)↓(6)"; // s = s.replaceAll("[(]|[)]","").replaceAll("[^\\d|↓|↑]",""); System.out.println(s); int i=0; List l1 = new ArrayList(); List l2 = new ArrayList(); int j; while((j = s.indexOf("↑",i))!=-1){ l1.add(s.charAt(j+2)); i=j+2; } System.out.println(l1); i = 0; while((j = s.indexOf("↓",i))!=-1){ l2.add(s.charAt(j+2)); i=j+2; } System.out.println(l2);
public static void main(String[] args) {
String str = "马五↑(3)↓(1)↑(4)↓(2)↓(6)";
int up = getInt(str, "(?<=↑\\()\\d(?=\\))"); //上箭头
int down = getInt(str, "(?<=↓\\()\\d(?=\\))"); //下箭头
System.out.println(up);
System.out.println(down);
}
public static int getInt(String str, String regex){
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
String result = "";
while(m.find()){
result += m.group();
}
return Integer.parseInt(result);
}
String s1="",s2="";
Matcher m;
m = Pattern.compile("↑\\((\\d*)\\)").matcher("马五↑(3)↓(1)↑(4)↓(2)↓(6)");
while(m.find()) {
s1 += m.group(1);
}
m = Pattern.compile("↓\\((\\d*)\\)").matcher("马五↑(3)↓(1)↑(4)↓(2)↓(6)");
while(m.find()) {
s2 += m.group(1);
}
System.out.println(s1);
System.out.println(s2);
s = s.replaceAll("[(]|[)]","").replaceAll("[^\\d|↓|↑]","");
System.out.println(s);
int i=0;
List l1 = new ArrayList();
List l2 = new ArrayList();
int j;
while((j = s.indexOf("↑",i))!=-1){
l1.add(s.charAt(j+1));
i=j+1;
}
System.out.println(l1);
i = 0;
while((j = s.indexOf("↓",i))!=-1){
l2.add(s.charAt(j+1));
i=j+1;
}
System.out.println(l2);
public static void main(String[] args) {
String str = "马五↑(3)↓(1)↑(4)↓(2)↓(6)";
String zh = str.replaceAll("[^\u4E00-\u9FFF]", "");
String down = str.replaceAll("[\u4E00-\u9FFF]", "").replaceAll("↑\\(\\d\\)", "").replaceAll("↓\\((\\d)\\)", "$1");
String up = str.replaceAll("[\u4E00-\u9FFF]", "").replaceAll("↓\\(\\d\\)", "").replaceAll("↑\\((\\d)\\)", "$1");
System.out.println(zh);
System.out.println(up);
System.out.println(down);
}
。"[(]|[)]","").replaceAll("[^\\d|↓|↑]",这不是正则式什么
后面就没改了 index +1 改成+2就可以了
String s = "马五↑(3)↓(1)↑(4)↓(2)↓(6)";
// s = s.replaceAll("[(]|[)]","").replaceAll("[^\\d|↓|↑]","");
System.out.println(s);
int i=0;
List l1 = new ArrayList();
List l2 = new ArrayList();
int j;
while((j = s.indexOf("↑",i))!=-1){
l1.add(s.charAt(j+2));
i=j+2;
}
System.out.println(l1);
i = 0;
while((j = s.indexOf("↓",i))!=-1){
l2.add(s.charAt(j+2));
i=j+2;
}
System.out.println(l2);