如何在Java中使用正则表达式进行删除

huangapple go评论102阅读模式
英文:

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

[
  {
    &quot;description&quot;: &quot;&#220;ntuk testing&quot;,
    &quot;is_multiple_apply&quot;: true,
    &quot;invoice&quot;: &quot;invoice-Test&quot;,
    &quot;fc_code&quot;: &quot;invoice-fc-test&quot;,
    &quot;promo_min_purchase_amount&quot;: 300,
    &quot;promo_min_purchase_qty&quot;: 3,
    &quot;promo_max_qty_applied&quot;: 2,
    &quot;promo_date_end&quot;: &quot;2020-08-15T23:59:00+0000&quot;,
    &quot;promo_date_start&quot;: &quot;2020-08-01T00:00:00+0000&quot;,
    &quot;product_promo&quot;: [
      &quot;{&quot;plu&quot;:100,&quot;qty&quot;:100,&quot;discount&quot;:100,&quot;discount_type&quot;:&quot;AMOUNT&quot;}&quot;
    ]
  }
]

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&lt;ProductsPromoModel&gt; getProductsPromo() throws JsonMappingException, JsonProcessingException{
	    ObjectMapper mapper = new ObjectMapper();
	    List&lt;ProductsPromoModel&gt; productPromoMods = new ArrayList&lt;&gt;();
	    
	    productPromoMods = mapper.readValue(this.productsPromo.toString(), new TypeReference&lt;List&lt;ProductsPromoModel&gt;&gt;(){});
	      
	    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(&quot;\&quot;(?=\\{)|(?&lt;=\\})\&quot;&quot;, &quot;&quot;);

Or you can do two replacements that are a little less complicated:

yourString.replaceAll(&quot;\&quot;\\{&quot;, &quot;{&quot;).replaceAll(&quot;\\}\&quot;&quot;, &quot;}&quot;)

huangapple
  • 本文由 发表于 2020年7月22日 16:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63029581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定