Java模式匹配器错误地计算正则表达式

Java模式匹配器错误地计算正则表达式,java,regex,Java,Regex,我正在搜索一个引号,后跟给定字符串中的四位数字、引号和冒号——如果我的东西是直接的话,应该是regex(“\d{4}”):。但是,以下是我的代码: Pattern regex = Pattern.compile("(\"[0-9]{4}\":)"); System.out.println("Regex: " + regex.pattern()); Matcher matcher = regex.matcher(itemJson); String itemId; String toReplace;

我正在搜索一个引号,后跟给定字符串中的四位数字、引号和冒号——如果我的东西是直接的话,应该是regex(“\d{4}”):。但是,以下是我的代码:

Pattern regex = Pattern.compile("(\"[0-9]{4}\":)");
System.out.println("Regex: " + regex.pattern());
Matcher matcher = regex.matcher(itemJson);
String itemId;
String toReplace;
String replaceWith;
int lastIndex;
while(matcher.find()){
    lastIndex = matcher.start(); 
    itemId = itemJson.substring(lastIndex+1, lastIndex+5);
    toReplace = itemJson.substring(lastIndex, lastIndex+8);
    replaceWith = "{ \"id\":" + itemId + ",";
    System.out.println(itemJson.substring(matcher.start(), matcher.end()));
    itemJson = itemJson.replace(toReplace, replaceWith);
}
打印出以下内容:

Regex: "[0-9]{4}":
"1001":
0}},"10
":0.6}}
nMod":1
老实说,我不知道出了什么问题。。。我正在将EclipseKepler与JavaSE1.6结合使用,以防万一

EDIT:itemJson是从服务器获取的传递给此类的字符串。对于上面的输出,我使用了一个缩写版本:

"data":{"1001":{"name":"Boots of Speed","plaintext":"Slightly increases Movement Speed","group":"BootsNormal","description":"<groupLimit>Limited to 1.</groupLimit><br><br><unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br><i>(Unique Passives with the same name don't stack.)</i>","colloq":";","into":["3006","3047","3020","3158","3111","3117","3009"],"image":{"full":"1001.png","sprite":"item0.png","group":"item","x":0,"y":0,"w":48,"h":48},"gold":{"base":325,"total":325,"sell":227,"purchasable":true},"tags":["Movement","Boots"],"stats":{"FlatMovementSpeedMod":25.0}},"1004":{"name":"Faerie Charm","plaintext":"Slightly increases Mana Regen","description":"<stats>+3 Mana Regen per 5 seconds</stats>","colloq":";","into":["3152","3028","3070","3073","1080","3069","3165"],"image":{"full":"1004.png","sprite":"item0.png","group":"item","x":48,"y":0,"w":48,"h":48},"gold":{"base":180,"total":180,"sell":126,"purchasable":true},"tags":["ManaRegen"],"stats":{"FlatMPRegenMod":0.6}},"1006":{"name":"Rejuvenation Bead","plaintext":"Slightly increases Health Regen","description":"<stats>+5 Health Regen per 5 seconds</stats>","colloq":";","into":["3077","3097","3112","3083","1080","2051","3105"],"image":{"full":"1006.png","sprite":"item0.png","group":"item","x":96,"y":0,"w":48,"h":48},"gold":{"base":180,"total":180,"sell":126,"purchasable":true},"tags":["HealthRegen"],"stats":{"FlatHPRegenMod":1.0}},"1011":{"name":"Giant's Belt","plaintext":"Greatly increases Health","description":"<stats>+380 Health</stats>","colloq":";","into":["3083","3022","3068","3116","3084","3143"],"image":{"full":"1011.png","sprite":"item0.png","group":"item","x":144,"y":0,"w":48,"h":48},"gold":{"base":1000,"total":1000,"sell":700,"purchasable":true},"tags":["Health"],"stats":{"FlatHPPoolMod":380.0}}}
“数据”:{“1001”:{“名称”:“速度靴”,“明文”:“略微提高移动速度”,“组”:“BootsNormal”,“描述”:“限制为1.

唯一被动-增强移动:+25移动速度

(唯一同名被动词不叠加),“口语”:“,”进入“[“3006”,“3047”,“3020”,“3158”,“3111”,“3117”,“3009”,“image”:”{“完整”:“1001.png”,“精灵”:“item0.png”,“团体”:“item”,“x”:0,“y”:0,“w”:48,“h”:48},“黄金”:{“基本”:325,“总计”:325,“出售”:227,“可购买”:true},“标签”:“移动”,“靴子”;“统计”:{“FlatMovementSpeedMod”:25.0},“1004”:{“姓名”:“精灵魅力”,“明文”:“略微增加法力回复”,“说明”:“+3法力回复每5秒”,“口语化”:“;“进入”:”3152,“3028”,“3070”,“3073”,“1080”,“3069”,“3165”],“图像”:{“完整”:“1004.png”,“精灵”:“item0.png”,“组”:“item”,“x”:48,“y”:0,“w”:48,“h”:48},“黄金”:{“base”:180,“总计”:180,“销售”:126,“可购买”:true},“标签”:[“ManaRegen”],“stats”:{“FlatMPRegenMod 0.6},“1006”:{“名称”:“返老还童珠”,“明文”:“略微增加健康回复”,“描述”:“说明”“:”:“+5每5秒回复生命值“,”口语“;”,“,”进入“:”[“3077”,“3097”,“3112”,“3083”,“1080”,“2051”,“3105“,”图像“:”{”完整“:“1006.png”,“精灵“:”item0.png“,”组“:”项目“,”x“:96,“y“:”0,“w“,”48,“h“,”黄金“:”:{“基本”:180,“总计”:180,“出售”:126,“可购买”:真的“,”标签“:”[“健康回复“,”统计信息“:{”flathpregen 1.0:”1.0“,”名称“,”巨型腰带“,”,“纯文本”:“大大增加健康”,“描述”:“+380健康”,“口语”:“;”,“进入”:[“3083”,“3022”,“3068”,“3116”,“3084”,“3143”],“图像”:{“完整”:“1011.png”,“sprite”:“item0.png”,“group”:“item”,“x”:144,“y”:0,“w”:48,“h”:48},“gold”:{“base”:1000,“total”:1000,“sell”:700,“purchable”:true},“tags”:“Health”;“stats”:“{“flathpoolmod 380.0}”

谢谢@aliteralmind

您没有在每次while迭代结束时重置匹配器。修复:

import  java.util.regex.Matcher;
import  java.util.regex.Pattern;

/**
   <P>{@code java RegexTest}</P>
 **/
public class RegexTest  {
   public static final void main(String[] igno_red)  {
        String itemJson = "{\"data\":{{ \"id\":1001,\"name\":\"Boots of Speed\",\"plaintext\":\"Slightly increases Movement Speed\",\"group\":\"BootsNormal\",\"description\":\"<groupLimit>Limited to 1.</groupLimit><br><br><unique>UNIQUE Passive - Enhanced Movement:</unique> +25 Movement Speed<br><br><i>(Unique Passives with the same name don't stack.)</i>\",\"colloq\":\";\",\"into\":[\"3006\",\"3047\",\"3020\",\"3158\",\"3111\",\"3117\",\"3009\"],\"image\":{\"full\":\"1001.png\",\"sprite\":\"item0.png\",\"group\":\"item\",\"x\":0,\"y\":0,\"w\":48,\"h\":48},\"gold\":{\"base\":325,\"total\":325,\"sell\":227,\"purchasable\":true},\"tags\":[\"Movement\",\"Boots\"],\"stats\":{\"FlatMovementSpeedMod\":25.{ \"id\":}},\",4\":{\"name\":\"Faerie Charm\",\"plaintext\":\"Slightly increases Mana Regen\",\"description\":\"<stats>+3 Mana Regen per 5 seconds</stats>\",\"colloq\":\";\",\"into\":[\"3152\",\"3028\",\"3070\",\"3073\",\"1080\",\"3069\",\"3165\"],\"image\":{\"full\":\"1004.png\",\"sprite\":\"item0.png\",\"group\":\"item\",\"x\":48,\"y\":0,\"w\":48,\"h\":48},\"gold\":{\"base\":180,\"total\":180,\"sell\":126,\"purchasable\":true},\"tags\":[\"ManaRegen\"],\"stats\":{\"FlatMPRegenMod{ \"id\"::0.6,\"1006\":{\"name\":\"Rejuvenation Bead\",\"plaintext\":\"Slightly increases Health Regen\",\"description\":\"<stats>+5 Health Regen per 5 seconds</stats>\",\"colloq\":\";\",\"into\":[\"3077\",\"3097\",\"3112\",\"3083\",\"1080\",\"2051\",\"3105\"],\"image\":{\"full\":\"1006.png\",\"sprite\":\"item0.png\",\"group\":\"item\",\"x\":96,\"y\":0,\"w\":48,\"h\":48},\"gold\":{\"base\":180,\"total\":180,\"sell\":126,\"purchasable\":true},\"tags\":[\"HealthRegen\"],\"stats\":{\"FlatHPRege{ \"id\":Mod\",0}},\"1011\":{\"name\":\"Giant's Belt\",\"plaintext\":\"Greatly increases Health\",\"description\":\"<stats>+380 Health</stats>\",\"colloq\":\";\",\"into\":[\"3083\",\"3022\",\"3068\",\"3116\",\"3084\",\"3143\"],\"image\":{\"full\":\"1011.png\",\"sprite\":\"item0.png\",\"group\":\"item\",\"x\":144,\"y\":0,\"w\":48,\"h\":48},\"gold\":{\"base\":1000,\"total\":1000,\"sell\":700,\"purchasable\":true},\"tags\":[\"Health\"],\"stats\":{\"FlatHPPoolMod\":380.0}}}";

      Pattern regex = Pattern.compile("(\"[0-9]{4}\":)");
      System.out.println("Regex: " + regex.pattern());
      Matcher matcher = regex.matcher(itemJson);
      String itemId;
      String toReplace;
      String replaceWith;
      int lastIndex;
      while(matcher.find()){
         lastIndex = matcher.start();
         itemId = itemJson.substring(lastIndex+1, lastIndex+5);
System.out.println("-itemId=\"" + itemId + "\"");
         toReplace = itemJson.substring(lastIndex, lastIndex+8);
System.out.println("-toReplace=\"" + toReplace + "\"");
         replaceWith = "{ \"id\":" + itemId + ",";
System.out.println("-replaceWith=\"" + replaceWith + "\"");
         System.out.println(itemJson.substring(matcher.start(), matcher.end()));
         itemJson = itemJson.replace(toReplace, replaceWith);

         //Must reset the matcher for the updated string!
         matcher.reset(itemJson);
      }
   }
}

显示输入字符串yes
(\d{4}:)
应该可以。试穿-为什么不使用字符串。替换?你确定模式匹配器的计算错误吗?可能是因为你对它的理解不正确吗?只是一个想法。为什么要使用正则表达式?为什么不使用专用的JSON解析器?@aliteralmind我添加了一个示例输入字符串,对不起!这很有效,非常感谢!我知道我被遗漏了愚蠢的事
[C:\java_code\]java RegexTest
Regex: ("[0-9]{4}":)
      -itemId="1006"
      -toReplace=""1006":{"
      -replaceWith="{ "id":1006,"
"1006":
      -itemId="1011"
      -toReplace=""1011":{"
      -replaceWith="{ "id":1011,"
"1011":