大神们,求一个可以匹配数字、字母、小数点、负数之间加减乘除的正则表达式, 在线等!!!!

qq_36195640 2016-09-21 08:59:07
比如-3.2a+4.5c/3-4c*3d 。。
...全文
457 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
rickylin86 2016-09-21
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args){
		String content = "-3.2a+4.5c/3-4c*3d";
		String regex = "-?\\d+[.]?\\d*[A-Za-z]*([-+*/]\\d+[.]?\\d*[A-Za-z]*)+";//字母是否必须是最后??
		Matcher matcher = Pattern.compile(regex).matcher(content);
		System.out.println(matcher.matches());
	}
}
bichir 2016-09-21
  • 打赏
  • 举报
回复
[a-zA-Z0-9\+\-\*\\\.]
逗泥丸的平方 2016-09-21
  • 打赏
  • 举报
回复
你还是自己学一下吧. 也不用学 看一眼符号是什么就好了
qq_36195640 2016-09-21
  • 打赏
  • 举报
回复
这个肯定不对呀,不能匹配字母,小数点也不行把
soton_dolphin 2016-09-21
  • 打赏
  • 举报
回复
[{d} ][-+*/][{d} ]
soton_dolphin 2016-09-21
  • 打赏
  • 举报
回复
你想要这个 [-+*/]?
rickylin86 2016-09-21
  • 打赏
  • 举报
回复
表示字母的数量没有限制,但是如果出现的话必须放在最后出现.
qq_36195640 2016-09-21
  • 打赏
  • 举报
回复
引用 6 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args){
		String content = "-3.2a+4.5c/3-4c*3d";
		String regex = "-?\\d+[.]?\\d*[A-Za-z]*([-+*/]\\d+[.]?\\d*[A-Za-z]*)+";//字母是否必须是最后??
		Matcher matcher = Pattern.compile(regex).matcher(content);
		System.out.println(matcher.matches());
	}
}
最后一位不用是字母,这个正则是否还须修改?
qq_36195640 2016-09-21
  • 打赏
  • 举报
回复
引用 6 楼 rickylin86 的回复:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args){
		String content = "-3.2a+4.5c/3-4c*3d";
		String regex = "-?\\d+[.]?\\d*[A-Za-z]*([-+*/]\\d+[.]?\\d*[A-Za-z]*)+";//字母是否必须是最后??
		Matcher matcher = Pattern.compile(regex).matcher(content);
		System.out.println(matcher.matches());
	}
}
非常感谢

62,614

社区成员

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

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