请问怎么用正则表达式取出这两个字串?

endlesspass 2012-04-20 11:06:43
字符串“getSelectIndex(document.form1.insurantIDType_group, "01") ”或“getSelectIndex(document.form1.insurantIDType_group, '01')”;

求写一个正则表达式能取出子串:insurantIDType_group和 01?

请问怎么用正则表达式取出这两个字串?
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-04-21
  • 打赏
  • 举报
回复

public static void main(String args[]) throws Exception {
String s = "getSelectIndex(document.form1.insurantIDType_group, \"01\")getSelectIndex(document.form1.insurantIDType_group, '01')";
Matcher m = Pattern.compile("(.+?)\\(.+?,\\s*(\"|')(.+?)(\"|')\\)").matcher(s);
while(m.find()){
System.out.println(m.group(1));
System.out.println(m.group(3));
}
}
挨踢直男 2012-04-20
  • 打赏
  • 举报
回复
"^.+?form1\\.(.+?),\\s?(['\"])(\\d+)\\2\\)$"
这样?
endlesspass 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

正则基本都通用的。
/^.+?form1\.(.+?),\s?(['"])(\d+)\2\)$/i
[/Quote]

你用JAVA写下就知道了,,这里面有好多转义字符,转来转去,也挺麻烦,,还真没转义好。。
endlesspass 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

正则基本都通用的。
/^.+?form1\.(.+?),\s?(['"])(\d+)\2\)$/i
[/Quote]
是呀,但这样的语法,\ " \,在JAVA里通不过呀,,
求JAVA写法。。。。
001007009 2012-04-20
  • 打赏
  • 举报
回复
正则基本都通用的。
/^.+?form1\.(.+?),\s?(['"])(\d+)\2\)$/i
endlesspass 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

JScript code

var str = 'getSelectIndex(document.form1.insurantIDType_group, "01")';
var str1 = "getSelectIndex(document.form1.insurantIDType_group, '01')";
var r……
[/Quote]


我没描述清楚,楼上的写的应该不错,请问下JAVA的怎么写?
001007009 2012-04-20
  • 打赏
  • 举报
回复

var str = 'getSelectIndex(document.form1.insurantIDType_group, "01")';
var str1 = "getSelectIndex(document.form1.insurantIDType_group, '01')";
var re = /^.+?form1\.(.+?),\s?(['"])(\d+)\2\)$/i;
alert(str.match(re)[1])
alert(str.match(re)[3])

alert(str1.match(re)[1])
alert(str1.match(re)[3])

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧