java 正则求解,100分 在线急盼!!!

littleboys 2008-04-19 05:56:51
问题是这样的:

现在的有一个字符串,以逗号分割的2位或者1 位数字
例如:
values="1,12,16,56"

输入一个1 到2 位的数字字符,判断是否在存在于上面的字符中
只有 1 12 16 56 这4个数字符合条件,其它的都不符合。

怎样写这样的一个正则表达式?


...全文
545 点赞 收藏 67
写回复
67 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodmrning 2008-04-22
学习了
回复
&永无止境 2008-04-22
Pattern p = Pattern.compile("1|12|16|56");
String input = "16";
System.out.println(p.matcher(input).matches());
input = "1";
System.out.println(p.matcher(input).matches());
input = "12";
System.out.println(p.matcher(input).matches());
input = "56";
System.out.println(p.matcher(input).matches());

input = "1156";
System.out.println(p.matcher(input).matches());
input = "5";
System.out.println(p.matcher(input).matches());
input = "11";
System.out.println(p.matcher(input).matches());
input = "26";
System.out.println(p.matcher(input).matches());
input = "121";
System.out.println(p.matcher(input).matches());
input = "55";
System.out.println(p.matcher(input).matches());
输出:
true
true
true
true
false
false
false
false
false
false
回复
puppyli 2008-04-22
学习学习
回复
puppyli 2008-04-22
学习,学习啊
回复
KKK2007 2008-04-22
[Quote=引用 19 楼 littleboys 的回复:]
上面的代码怎么乱了,再贴一次
Java codepublicclassCheckIn {/***@paramargs*/publicstaticvoidmain(String[] args) {

String type_id="1,2,9,13,17,14,56";
System.out.println("return1="+CheckIn.isIn(type_id,"1"));
System.out.println("return2="+CheckIn.isIn(type_id,"2"));
System.out.println("return3="+CheckIn.isIn(type_id,"3"));

System.out.println("retur…
[/Quote]
回复
我是风 2008-04-22
import java.util.Scanner;

public final class InputNumberTester {
private static String values = "1,12,16,56";

public static boolean Test(String numberStr) {
try {
Integer.parseInt(numberStr);
return InputNumberTester.values.contains(numberStr);
} catch (Exception e) {
return false;
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString = "";
while(!inputString.equalsIgnoreCase("quit")) {
inputString = scanner.next();
System.out.println(InputNumberTester.Test(inputString));
}
}
}
回复
cyj18 2008-04-22
正则不是很清楚..但是用代码实现是OK的.
回复
cyj18 2008-04-22
给我分....我要分...
回复
cyj18 2008-04-22
import java.util.regex.Pattern;


public class Test
{
public static void main(String[] args)
{
String old = "1,12,16,56";
String value = "21";
System.out.println(checked(value, old));

}


public static boolean checked(String str,String old)
{
if(str.indexOf(',')== -1)
{
Pattern pa = Pattern.compile(str);
return pa.matcher(old).find();
}
return false;
}
}
回复
gzdiablo 2008-04-22
不好意思 发现java下正则表达式的方法和.net有点不同 下面是indexOf的方法

boolean xxx(String str,String aa){
return ("," + str + ",").indexOf("," + aa + ",") > -1;
}
回复
tombow 2008-04-22
没必要用正则啊.
回复
littleboys 2008-04-22
50 楼了,谢谢大家的关注,我把帖子又加了100 分,散散分。
回复
haoxiongok 2008-04-22
我来帮楼主顶上去
回复
littleboys 2008-04-22
看来也没有新的方法了。
总结一下:

//12,23,38,53 楼
boolean check1(String str,String input){
return ("," + str + ",").indexOf("," + input+ ",") > -1;
}
//11 楼
boolean check2(String str,String input){
return str.matches(".*\\"+input+"\\b.*"));
}
//24,27 ...
boolean check3(String str,String input){
return input.matches(str.replaceAll(",","|"));
}

总体感觉这三个方法比较好
正向(24)、逆向思维(11楼)都有

总结完毕



回复
&永无止境 2008-04-22
如果需要,可以再加些对str,input的验证
public class CheckIn {

/**
* @param args
*/
public static void main(String[] args) {

String type_id ="1,2,9,13,17,14,56";
System.out.println("return 1="+CheckIn.isIn(type_id,"1"));
System.out.println("return 2="+CheckIn.isIn(type_id,"2"));
System.out.println("return 3="+CheckIn.isIn(type_id,"3"));

System.out.println("return 4="+CheckIn.isIn(type_id,"4"));
System.out.println("return 13="+CheckIn.isIn(type_id,"13"));
System.out.println("return 5="+CheckIn.isIn(type_id,"5"));
System.out.println("return 6="+CheckIn.isIn(type_id,"6"));

System.out.println("return 11 1="+CheckIn.isIn("11","1"));
System.out.println("return 1 11="+CheckIn.isIn("1,11","11"));
System.out.println("return 55 58="+CheckIn.isIn("55,58","4"));
System.out.println("return ,58="+CheckIn.isIn(",58","58"));
System.out.println("return , 58="+CheckIn.isIn(" , 58","58"));
System.out.println("return q,58="+CheckIn.isIn("q,58","58"));
System.out.println("return 5,58="+CheckIn.isIn("5,58","5"));
System.out.println("return 5,58="+CheckIn.isIn("5,58","f11"));
System.out.println("return ,5,8,="+CheckIn.isIn(",5,8,","8"));

}

public static boolean isIn(String str,String input)
{
//如果需要,加上str和input的验证
String reg_str = "((\\d{1,2})?,?)+";
String reg_input = "\\d+";
System.out.print("str(" + str + ") is Ok?" + str.matches(reg_str) + "; input(" + input + ") is ok?" + input.matches(reg_input) + " ");
if (!str.matches(reg_str)) {
return false;
}
if (!input.matches(reg_input)) {
return false;
}
String newStr = str.replaceAll(",", "|");
return input.matches(newStr);
}

}
输出:
str(1,2,9,13,17,14,56) is Ok?true; input(1) is ok?true return 1=true
str(1,2,9,13,17,14,56) is Ok?true; input(2) is ok?true return 2=true
str(1,2,9,13,17,14,56) is Ok?true; input(3) is ok?true return 3=false
str(1,2,9,13,17,14,56) is Ok?true; input(4) is ok?true return 4=false
str(1,2,9,13,17,14,56) is Ok?true; input(13) is ok?true return 13=true
str(1,2,9,13,17,14,56) is Ok?true; input(5) is ok?true return 5=false
str(1,2,9,13,17,14,56) is Ok?true; input(6) is ok?true return 6=false
str(11) is Ok?true; input(1) is ok?true return 11 1=false
str(1,11) is Ok?true; input(11) is ok?true return 1 11=true
str(55,58) is Ok?true; input(4) is ok?true return 55 58=false
str(,58) is Ok?true; input(58) is ok?true return ,58=true
str( , 58) is Ok?false; input(58) is ok?true return , 58=false
str(q,58) is Ok?false; input(58) is ok?true return q,58=false
str(5,58) is Ok?true; input(5) is ok?true return 5,58=true
str(5,58) is Ok?true; input(f11) is ok?false return 5,58=false
str(,5,8,) is Ok?true; input(8) is ok?true return ,5,8,=true

回复
&永无止境 2008-04-22
24的不应该加[],加了[]他认为[]里的是一为数字,就与题目本意差远了,还是发个我的版本吧
public class CheckIn {

/**
* @param args
*/
public static void main(String[] args) {

String type_id ="1,2,9,13,17,14,56";
System.out.println("return 1="+CheckIn.isIn(type_id,"1"));
System.out.println("return 2="+CheckIn.isIn(type_id,"2"));
System.out.println("return 3="+CheckIn.isIn(type_id,"3"));

System.out.println("return 4="+CheckIn.isIn(type_id,"4"));
System.out.println("return 13="+CheckIn.isIn(type_id,"13"));
System.out.println("return 5="+CheckIn.isIn(type_id,"5"));
System.out.println("return 6="+CheckIn.isIn(type_id,"6"));

System.out.println("return 11 1="+CheckIn.isIn("11","1"));
System.out.println("return 1 11="+CheckIn.isIn("1","11"));
System.out.println("return 55 58="+CheckIn.isIn("55,58","4"));

}

public static boolean isIn(String str,String input)
{
String newStr = str.replaceAll(",", "|");
return input.matches(newStr);
}

}

输出:
return 1=true
return 2=true
return 3=false
return 4=false
return 13=true
return 5=false
return 6=false
return 11 1=false
return 1 11=false
return 55 58=false

不知道楼主要的是不是这个效果?
回复
&永无止境 2008-04-22
哦,前面没理解楼主的意思,下面改进如下:
哎呀,已经有人写出来了,直接用24楼的就行了
回复
[Quote=引用 42 楼 gzdiablo 的回复:]
LS还须努力 最好别抄11楼的 11楼的还不算很好 只是比其它人都强些

其实 按照lz的要求 下面就行了


JScript codeboolean xxx(String str,String aa){
return str.matches("\\b" + aa + "\\b");
}
[/Quote]
当然是没看他的了 要不然也不会用那么撮的表达式了
return str.matches(".*\\b" + aa + "\\b.*");
回复
gjgj3368 2008-04-21
学习
回复
wuyetingyu 2008-04-21
[Quote=引用 42 楼 gzdiablo 的回复:]
LS还须努力 最好别抄11楼的 11楼的还不算很好 只是比其它人都强些

其实 按照lz的要求 下面就行了


JScript codeboolean xxx(String str,String aa){
return str.matches("\\b" + aa + "\\b");
}
[/Quote]
能讲详细一点吗,把测试的部分写完,偶很菜的
回复
发动态
发帖子
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
社区公告
暂无公告