英文:
How to remove with Regex In Java
问题
Sure, here's the translation of the provided content:
我有一段以字符串形式表示的示例数据
[
{
"description": "测试测试",
"is_multiple_apply": true,
"invoice": "invoice-测试",
"fc_code": "invoice-fc-测试",
"promo_min_purchase_amount": 300,
"promo_min_purchase_qty": 3,
"promo_max_qty_applied": 2,
"promo_date_end": "2020-08-15T23:59:00+0000",
"promo_date_start": "2020-08-01T00:00:00+0000",
"product_promo": [
"{\"plu\":100,\"qty\":100,\"discount\":100,\"discount_type\":\"AMOUNT\"}"
]
}
]
然后在 **product_promo** 对象中确切地在这里
> "{\"plu\":100,\"qty\":100,\"discount\":100,\"discount_type\":\"AMOUNT\"}"
* **花括号外面有一个引号**,我想要去掉它
这是一种将先前的字符串数据转换为可用模型 `ProductsPromoModel` 的工作方法
public List<ProductsPromoModel> getProductsPromo() throws JsonMappingException, JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
List<ProductsPromoModel> productPromoMods = new ArrayList<>();
productPromoMods = mapper.readValue(this.productsPromo.toString(), new TypeReference<List<ProductsPromoModel>>(){});
return productPromoMods;
}
但因为花括号外面有引号。所以会出现错误
如何使用正则表达式处理?
英文:
I have a sample data in the form of a string
[
{
"description": "Üntuk testing",
"is_multiple_apply": true,
"invoice": "invoice-Test",
"fc_code": "invoice-fc-test",
"promo_min_purchase_amount": 300,
"promo_min_purchase_qty": 3,
"promo_max_qty_applied": 2,
"promo_date_end": "2020-08-15T23:59:00+0000",
"promo_date_start": "2020-08-01T00:00:00+0000",
"product_promo": [
"{"plu":100,"qty":100,"discount":100,"discount_type":"AMOUNT"}"
]
}
]
then in the product_promo object exactly here
> "{"plu":100,"qty":100,"discount":100,"discount_type":"AMOUNT"}"
- there is a quote outside the curly braces and I want to get rid of it
This is a working method for converting previous string data into available model ProductsPromoModel
public List<ProductsPromoModel> getProductsPromo() throws JsonMappingException, JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
List<ProductsPromoModel> productPromoMods = new ArrayList<>();
productPromoMods = mapper.readValue(this.productsPromo.toString(), new TypeReference<List<ProductsPromoModel>>(){});
return productPromoMods;
}
but because there are quotes outside the curly braces. so the process occurs error
how to do it with regex?
答案1
得分: 1
你可以使用正则表达式的前瞻和后顾来完成这个操作,如下所示:
yourString.replaceAll("\\\\\"(?=\\{)|(?<=\\})\\\\\"", "");
或者你可以进行两次替换,稍微简单一些:
yourString.replaceAll("\\\\\"\\{\\\\\"", "{").replaceAll("\\\\}\"\\\\\"", "}");
英文:
You can do this with regex lookaheads and lookbehinds as follows:
yourString.replaceAll("\"(?=\\{)|(?<=\\})\"", "");
Or you can do two replacements that are a little less complicated:
yourString.replaceAll("\"\\{", "{").replaceAll("\\}\"", "}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论