求助。小问题。字符串读值 String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";如果把字符串里的()里面的password值全读出来? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String addr = "user1(password1),user2(password2),user3(password3),user4(password4),"; String regex = "\\(([^\\(\\)]+)\\)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(addr); while (m.find()) System.out.println(m.group(1)); public static void main(String[] args) { String addr = "user1(password1),user2(password2),user3(password3),user4(password4),"; Matcher m = Pattern.compile("(\\((.+?)\\))").matcher(addr); while (m.find()) { System.out.println(m.group(2)); } } 你可以用substring()和indexof()两个方法得到,大概如下:String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";int beginIndex = addr.indexOf("(");int endIndex = addr.indexOf(")");String password1 = addr.substring(beginIndex, endIndex);//得到password1String addr1 = addr.substring(endIndex+1);//此时addr1 ="user2(password2),user3(password3),user4(password4),";重复上面的方法,就可以得到password2,以此类推 试试这个public static void main(String[] args) { String regex = "(?<=\\()\\w+(?=\\))";//或者String regex = "(?<=\\()(.+?)(?=\\))",看你的password中是否含有非字母、数字、下划线的字符而定。 String str; String strOri = "user1(password1),user2(password2),user3(password3),user4(password4)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(strOri); while(matcher.find()) { str = matcher.group(); System.out.println(str); } } 可以呀,用正则匹配出来放到数组里面就可以了。就在那个while循环里面放进去。 public static void main(String[] args) { String addr = "user1(password1),user2(password2),user3(password3),user4(password4),"; String[] str = addr.split(","); for (int i = 0; i < str.length; i++) { str[i] = str[i].substring(str[i].indexOf("(")+1, str[i].length() - 1); } for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } 关于Java协变返回类型的一个问题 swing 界面布局求教 Java多态 必须满足的条件 struts2 + jasperreports 打印时出现红叉问题 java监控用户进程 请教,想将一个JTextField的值通过socket发送出去,应该如何写这个程序,请给大家给个实例,谢谢 小弟新学java,求书 用applet实现clock问题???????? 请高手帮我看看关于内嵌静态类的问题,谢谢 如何动态改变一个对象的部分状态? 关于SocketChannel的一个问题 用HtmlParser解析网页中文乱码问题
String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
String regex = "\\(([^\\(\\)]+)\\)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(addr);
while (m.find())
System.out.println(m.group(1));
String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
Matcher m = Pattern.compile("(\\((.+?)\\))").matcher(addr);
while (m.find()) {
System.out.println(m.group(2));
}
}
String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
int beginIndex = addr.indexOf("(");
int endIndex = addr.indexOf(")");
String password1 = addr.substring(beginIndex, endIndex);//得到password1String addr1 = addr.substring(endIndex+1);
//此时addr1 ="user2(password2),user3(password3),user4(password4),";重复上面的方法,就可以得到password2,以此类推
public static void main(String[] args) {
String regex = "(?<=\\()\\w+(?=\\))";//或者String regex = "(?<=\\()(.+?)(?=\\))",看你的password中是否含有非字母、数字、下划线的字符而定。
String str;
String strOri = "user1(password1),user2(password2),user3(password3),user4(password4)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strOri);
while(matcher.find())
{
str = matcher.group();
System.out.println(str);
}
}
String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
String[] str = addr.split(",");
for (int i = 0; i < str.length; i++) {
str[i] = str[i].substring(str[i].indexOf("(")+1, str[i].length() - 1);
}
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}