日期:2014-05-20  浏览次数:20635 次

求一条正则表达式
String   str   =   "id= 'fd,{}   s '   name= '45hd   324 '       ,width= 'sadf324 '   ,     type= 'hgf56 ',   style= 'asdf_234fd ',   height= '234_324 ' ";


在Java中用一条正则表达式,拆分出
id= 'fd,{}   s '
name= '45hd   324 '
width= 'sadf324 '
type= 'hgf56 '
style= 'asdf_234fd '
height= '234_324 '

就是求出一个元素(str)的属性(id)和属性值( 'fd,{}   s '),属性和属性之间可以用 ", "或者 "   "(空格)搁开,属性值中可以有 ", "、 "{} "等符号.

------解决方案--------------------
\w*= '\w*,?{?}?\s*\w* '
------解决方案--------------------
String str = "id= 'fd,{} s ' name= '45hd 324 ' ,width= 'sadf324 ' , type= 'hgf56 ', style= 'asdf_234fd ', height= '234_324 ' ";
Pattern p = Pattern.compile( "\\w+=\\ '[^=]+\\ ' ");
Matcher m = p.matcher(str);
int count = m.groupCount();
StringBuffer sb = new StringBuffer();
while (m.find()) {
for (int i = 0; i <= count; i++) {
sb.append(m.group(i));
sb.append( "\n ");
}
}
System.out.println(sb);
------解决方案--------------------
regex = "\\w+= '[^= ']+ '|\\w+=[^=]+ "
=====
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);

while (matcher.find()) {
// Get the match result
String result = matcher.group();
System.out.println(result);
}