How to extract big mgrs using regex

I have an input json:

{"id":12345,"mgrs":"04QFJ1234567890","code":"12345","user":"db3e1a-3c88-4141-bed3-206a"}

I would like to extract with regular expression MGRS of 1000 kilometer, in my example result should be: 04QFJ1267

First 2 symbols always digits, next 3 always chars and the rest always digits. MGRS have a fix length of 15 chars at all.

Is it possible?

Thanks.


All you really need to do is remove characters 8-10 and 13-15. If you want/need to do that using regex, then you could use the replace method with regex: ( EDIT Edited to remove the rest of the string).

 .*?(w{7})d{3}(d{2})d+.*

and replacement string:

$1$2

I see now you are using Java. So the relevant code line might look like:

resultString = subjectString.replaceAll(".*?(w{7})d{3}(d{2})d+.*", "$1$2");

The above assumes all your strings look like what you showed, and there is no need to test to be sure that "mgrs" is in the string.

链接地址: http://www.djcxy.com/p/87012.html

上一篇: 正则表达式从json字符串中提取名称和值

下一篇: 如何使用正则表达式提取大型mgr