regex to match substring after nth occurence of pipe character

前端 未结 3 962
有刺的猬
有刺的猬 2020-11-30 12:51

i am trying to build one regex expression for the below sample text in which i need to replace the bold text. So far i could achieve this much ((\\|))

3条回答
  •  被撕碎了的回忆
    2020-11-30 13:32

    Here's how you can do the replacement:

    String input = "1.1|ProvCM|111111111111|10.15.194.25|10.100.10.3|10.100.10.1|docsis3.0";
    int n = 3;
    String newValue = "new value";
    String output = input.replaceFirst("^((?:[^|]+\\|){"+n+"})[^|]+", "$1"+newValue);
    

    This builds:

    "1.1|ProvCM|111111111111|new value|10.100.10.3|10.100.10.1|docsis3.0"
    

提交回复
热议问题