A. java常用的几个正则方法
JAVA中正则表达式处理字符串的四个常用方法:匹配、分割、替换、截取。其跟字符串的常用函数相似,但是使用正则表达式会更简单、更加简洁。下面是具体的例子:
1 public class TestRegex {
2
3 public static void main(String[] args) {
4 String str = "";
5 String regex = "";
6
7 // 匹配
8 regex = "[1-9][a-z]";
9 getMatches(str, regex);
10
11 // 分割
12 str = "1a:abc123:";
13 regex = ":";
14 getSpilt(str, regex);
15
16 // 替换
17 str = "1223334444aaabbc";
18 String oldChar = "(.)\1+";
19 regex = "$1";
20 getReplace(str, oldChar, regex);
21
22 // 截取
23 str = "urlabc123";
24 regex = "(.*)";
25 getSubstring(str, regex);
26
27 }
28
29 public static void getMatches(String str, String regex) {
30 System.out.println(str.matches(regex));
31 }
32
33 public static void getSpilt(String str, String regex) {
34 String[] array = str.split(regex);
35 for (String t : array) {
36 System.out.println(t);
37 }
38 }
39
40 public static void getReplace(String str, String oldChar, String regex)
{
41 System.out.println(str.replaceAll(oldChar, regex));
42 }
43
44 public static void getSubstring(String str, String regex) {
45 Pattern p = Pattern.compile(regex);
46 Matcher m = p.matcher(str);
47 if (m.find()) {
48 System.out.println(m.group(1));
49 }
50 }
51 }
B. java正则表达式怎么书写
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
下面介绍具体的方法。
C. 如何用java写负数的正则表达式
Stringexpress="-3-4";
//express="-3+4";
//express="3+4";
//express="3-4";
//清除空格,避免无法匹配
express=express.replaceAll("","");
Matcherm=Pattern.compile("(-?\d{1,})+?(-?\d{1,})").matcher(express);
if(m.find()){
doubleone=Double.parseDouble(m.group(1));
doubletwo=Double.parseDouble(m.group(2));
System.out.println(one+two);
}
用正则就彻底一点,直接取结果计算, 正数、负数都当成一个数。
减法当作 加法 加负数就好,默认有个隐形的加号, 匹配并捕获完整的数字直接加法,不用自己在计算结果的时候写 负号。
D. java 如何获取double类型数据长度 如:123.12 长度为:3.2
String.split()字符串分割的参数是正则表达式,猜测你可能是因为这个所以分割出错了
publicclassTest{
publicstaticvoidmain(String[]args){
System.out.println(getLength(123.12));//3.2
System.out.println(getLength(23.6700));//2.2
System.out.println(getLength(15.00));//2.0
System.out.println(getLength(1234));//4.0
}
privatestaticStringgetLength(doublenum){
//注意15、15.000会被转成15.0,请自行判断是否符合要求
Stringstr=String.valueOf(num);
String[]nums=str.split("\.");//split方法的参数是正则表达式,所以.需要转义
intintLen=nums[0].length();
intdecimalLen=nums[1].equals("0")?0:nums[1].length();//15.0的情况特殊处理,小数长度设为0
//intdecimalLen=nums[1].length();//小数不特殊处理
returnintLen+"."+decimalLen;
}
}
E. 如何用正则表达式判断一个变量是否为double类型
将变量值转换为字符串,然后判断字符串是否符合下面的正则[0-9]*(.[0-9]*|[eE][+-][0-9]*)$
F. js验证double类型价格的正则表达式怎么写
只是double类型吗?保留几位小数?
这个可以验证带小数部分的小数和不带小数点的整数
<script>
function verify(node)
{
var str = node.value;
if(str.match(/^(:?(:?\d+.\d+)|(:?\d+))$/)) alert('match');
else alert('not match');
}
</script>
<input type=text id='txt' />
<input type=button onclick="verify(document.getElementById('txt'))" />
G. java中怎么用正则表达式表示数字(整数,小数等)
java表示整数和小数的正则表达式 ^[+-]?\d+(\.\d+)?$。
H. 速求java 用正则表达式 判断是否为String类型;int类型;Double类型 要有返回值
public class Test{
public String testStr = "3.1415926";
public double test(String testStr){
if(testStr.matches("\\d+\\.\\d+")){
return Double.valueOf(testStr);
}
return 0.0;
}
public static void main(String[] args){
Test t = new Test();
System.out.println(t.test("3.1415926"));
}
}
只写了浮点的,具体你查查资料大同小异,http://user.qzone.qq.com/359510920/blog/1308556402
具体的都有