繁体   English   中英

需要匹配以下正则表达式格式的字符串

[英]Need to match a string with following regex format

字符串格式为[( 1.0N)-195( 1.0E)-195(28)-769.7(NESW)-1080.8(U)-617.9(43-047-30127)]

我需要一个正则表达式进行匹配,以查看字符串是否包含-XXX-XXX (其中X是数字)

    Pattern p = Pattern.compile("(?=.*?(?:-[0-9][0-9][0-9]-[0-9][0-9][0-9]))");

    if(p.matcher(a).matches())

    {
        System.out.println("Matched");
    }

我也尝试过-[0-9][0-9][0-9]-[0-9][0-9][0-9](?=.*?-[0-9][0-9][0-9]-[0-9][0-9][0-9])无效

子字符串会容易得多,但是(?:\\\\d{2})(-\\\\d{3}-\\\\d{5})将匹配-XXX-XXXXX作为1组。

我假设最后一个数字中的3位数字是错误的。 如果不是,只需将5更改为3。

如果要检查字符串是否包含-3digits-3digits

 String a = "43-037-30149";
 Pattern p = Pattern.compile(".*(-[0-9]{3}-[0-9]{3})");
  if(p.matcher(a).matches())      
  {
     System.out.println("Matched");
  } 

你为什么不使用子串?

String b = a.substring(2,9); 

或这一个:

String c = a.substring(a.indexOf('-'), a.indexOf('-') + 8);

使“仅”成为子字符串也将更加有效! ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM