我写个正则 匹配 C301121212,就是C开头后面跟数字的
/**
* 返回是否以C开头,后面跟数字
* @param str
* @return
*/
public static boolean isStartWithC(String str)
{ //匹配以C开头的
Pattern p = Pattern.compile("[C][0-9]+");
Matcher m = p.matcher(str);
if (m.find())
{
return true; }
return false; }但是 像这种 30121.C301121212 怎么 也匹配上了,我想只匹配C开头后面跟数字的
/**
* 返回是否以C开头,后面跟数字
* @param str
* @return
*/
public static boolean isStartWithC(String str)
{ //匹配以C开头的
Pattern p = Pattern.compile("[C][0-9]+");
Matcher m = p.matcher(str);
if (m.find())
{
return true; }
return false; }但是 像这种 30121.C301121212 怎么 也匹配上了,我想只匹配C开头后面跟数字的
$结束
public static void main(String args[]){
String str = "30121.C301121212";
Pattern p = Pattern.compile("C\\d+");
Matcher m = p.matcher(str);
while (m.find())
System.out.println(m.group());
}
}
import java.util.regex.Pattern;
public class Test{
public static void main (String[] args)
{
String str="C301121212";
Pattern p = Pattern.compile("c([0-9]+)",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
if (m.find())
{
System.out.print(m.group());
}
}
}
这种跟
Pattern p = Pattern.compile("^[C][0-9]+$"); ^开始
$结束
没什么区别吧
public static void main(String args[]){
String str = "30121.C301121212";
String str1 = "C12343221";
String pattern = "C\\d+";
String pattern1 = "^[C][\\d]+";
System.out.println(str.matches(pattern));
System.out.println(str1.matches(pattern1));
System.out.println(str.matches(pattern1));
System.out.println(str1.matches(pattern));
}
}