逗号分隔字符串的正则表达式

时间:2014-10-01 10:43:27

标签: java regex

我正在尝试在Java中创建一个与以下字符串匹配的模式;

String message ="%%140911,A,140929100526,S0117.6262E03647.8107,000,067,F100,4F000100,108";

我形成的模式与字符串不匹配。我错过了什么?这是我迄今为止所尝试的模式:

private static final Pattern pattern = Pattern.compile(

       "(\\%\\%)"+"(\\d)," + // Id 
       "([AL])," + // Validity a for valid and l for invalid
       "(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})," + // Date (YYMMDD)Time (HHMMSS)
       "([NS])" + "(\\d{2})(\\d{2}\\.\\d+)" + "([EW])" + "(\\d{3})(\\d{2}\\.\\d+)," + //loc 
       "(\\d+)," + // Speed
       "(\\d+)," + // Direction
       "([FC])" + "(\\d{3})," + // temperature in Fahrenheit/celsius
       "(\\w{8})," + // status
       "(\\d+)"); // event

1 个答案:

答案 0 :(得分:3)

您在第一行中遗漏了+。尝试更改

"(\\%\\%)"+"(\\d),"

"(\\%\\%)"+"(\\d+),"