怎样用正则表达式从字符串中读取正负小数?例如:
jljjj> 1.24 <jlj> -1.5 <jdrdk> 0.0 <ggghd
谢谢大家
------解决方案--------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] argv) {
String s = "jljjj> 1.24 <jlj> -1.5 <jdrdk> 0.0 <ggghd ";
String sRegEx = "(-)?\\d+(.\\d*)? ";
Pattern pattern = Pattern.compile(sRegEx);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
------解决方案--------------------
public static void main(String[] args){
String a = "jljjj> 1.24 <jlj> -1.5 <jdrdk> 0.0 <ggghd ";
String[] b = new String[4];
String regExp= "-?\\d+\\.\\d+ ";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(a);
int i =0;
while(m.find()){
b[i] = a.substring(m.start(),m.end());
i++;
}
System.out.println(b[0]+b[1]+b[2]);
}
------解决方案--------------------
如果有一些稀奇古怪的,如0-1,1.-2这类的字符串夹在中间,还是用楼上两位的,如果格式如你给的例子里的,还是直观些的比较好,达到目的就行了。