这是一个非常奇怪的问题,但我无法找到具体的答案。
我正在创建一个Android应用程序,它接收一个跟踪号,然后确定要使用哪个API(FedEx / USPS)。
我对正则表达式的代码如下所示:
String USPSPattern1 = "/\\b((420 ?\\d\\d\\d\\d\\d ?)?(91|94|01|03|04|70|23|13)\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d( ?\\d\\d)?)\\b/i";
String USPSPattern2 = "/\\b((M|P[A-Z]?|D[C-Z]|LK|EA|V[A-Z]|R[A-Z]|CP|CJ|LC|LJ) ?\\d\\d\\d ?\\d\\d\\d ?\\d\\d\\d ?[A-Z]?[A-Z]?)\\b/i";
String FedExPattern1 = "/\\b((96\\d\\d\\d\\d\\d ?\\d\\d\\d\\d|96\\d\\d|\\d\\d\\d\\d) ?\\d\\d\\d\\d ?\\d\\d\\d\\d( ?\\d\\d\\d)?)\\b/i";
if(trackingNum.matches(USPSPattern1) || trackingNum.matches(USPSPattern2))
{
Log.d("USPS", "TRACKING NUMBER IS USPS");
mListener.onTrackingNumberSelected(trackingNum,1);
}
if(trackingNum.matches(FedExPattern1))
{
Log.d("FEDEX", "TRACKING NUMBER IS FEDEX");
mListener.onTrackingNumberSelected(trackingNum,2);
}
else
{
Log.d("INVALID", "TRACKING NUMBER IS NEITHER USPS OR FEDEX");
mListener.onTrackingNumberSelected(trackingNum,0);
}
我正在使用有效的USPS号进行测试,但出于某种原因,正则表达式不匹配。任何人都知道为什么会这样吗?
答案 0 :(得分:0)
开始时的一些提示。
\\d\\d\\d\\d
,请使用\\d{4}
/
希望这两件事能使模式更清晰,并让您找出问题所在。