Apache Camel /正则表达式替换字符

时间:2019-01-23 15:28:10

标签: regex apache-camel

我们通过oData查询返回了一个JSON字符串。

  1. 我们希望在诸如
    • @ odata.etag
    • msdyn_journaltype@OData.Community.Display.V1.FormattedValue
    • msdyn_endtime@OData.Community.Display.V1.FormattedValue
  2. 不更改电子邮件地址值,例如“ email”:“ bob@thebuilder.com”。

我在http://camel.apache.org/simple.html阅读了Apache Camel文档,可以使用

${in.body.replace('@', '')}

但是电子邮件地址也会被剥夺。

我当然可以分多个步骤完成,例如

${in.body.replace('msdyn_journaltype@OData.Community.Display.V1.FormattedValue', 'msdyn_journaltypeOData.Community.Display.V1.FormattedValue')}

但这可以做得更好,对吧? 我当时在考虑Apache Camel的“ regex”功能,但是对regex的经验并不丰富。

有人知道正则表达式的窍门吗?

{
  "@odata.context": "https:\/\/xxx.api.crm4.dynamics.com\/api\/data\/v9.0\/$metadata#bookableresourcebookings(name,msdyn_milestraveled,msdyn_actualarrivaltime,statecode,msdyn_workorder(foc_sapordernumber),Resource(name,foc_sapresourcenumber,email),msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking(msdyn_journaltype,msdyn_name,msdyn_starttime,msdyn_endtime,msdyn_duration,foc_sapstatisticalkeyfigurecod,statecode))",
  "@odata.count": 9,
  "value": [
    {
      "@odata.etag": "W\/\"5775823\"",
      "name": "Wartung 2011153  - Wartung - 181 L1205 Stuttgarter Str.\/Aichbr\u00fccke ",
      "msdyn_milestraveled@OData.Community.Display.V1.FormattedValue": "0,00",
      "msdyn_milestraveled": 0,
      "msdyn_actualarrivaltime@OData.Community.Display.V1.FormattedValue": "30.10.2018 08:55",
      "msdyn_actualarrivaltime": "2018-10-30T08:55:00Z",
      "statecode@OData.Community.Display.V1.FormattedValue": "Active",
      "statecode": 0,
      "bookableresourcebookingid": "bd90b7e6-18dc-e811-a95a-000d3a29f080",
      "msdyn_workorder": {
        "foc_sapordernumber": "856900132812"
      },
      "Resource": {
        "name": "Bob the Builder",
        "foc_sapresourcenumber": "00290035",
        "email": "bob@thebuilder.com"
      },
      "msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking": [
        {
          "@odata.etag": "W\/\"5775798\"",
          "msdyn_journaltype@OData.Community.Display.V1.FormattedValue": "Working Hours",
          "msdyn_journaltype": 690970000,
          "msdyn_name": "Wartung 2011153  - Wartung - 181 L1205 Stuttgarter Str.\/Aichbr\u00fccke ",
          "msdyn_starttime@OData.Community.Display.V1.FormattedValue": "29.10.2018 14:30",
          "msdyn_starttime": "2018-10-29T14:30:00Z",
          "msdyn_endtime@OData.Community.Display.V1.FormattedValue": "29.10.2018 16:00",
          "msdyn_endtime": "2018-10-29T16:00:00Z",
          "msdyn_duration@OData.Community.Display.V1.FormattedValue": "90",
          "msdyn_duration": 90,
          "foc_sapstatisticalkeyfigurecod": "",
          "statecode@OData.Community.Display.V1.FormattedValue": "Active",
          "statecode": 0,
          "_msdyn_booking_value@OData.Community.Display.V1.FormattedValue": "Wartung 2011153  - Wartung - 181 L1205 Stuttgarter Str.\/Aichbr\u00fccke ",
          "_msdyn_booking_value": "bd90b7e6-18dc-e811-a95a-000d3a29f080",
          "msdyn_bookingjournalid": "fd340afc-39dc-e811-a95a-000d3a29f080"
        },
        {
          "@odata.etag": "W\/\"5775795\"",
          "msdyn_journaltype@OData.Community.Display.V1.FormattedValue": "Travel",
          "msdyn_journaltype": 690970002,
          "msdyn_name": "Test WPLAN ohne Vertragstermine III  - Wartung - Bismarckplatz Ost ",
          "msdyn_starttime@OData.Community.Display.V1.FormattedValue": "29.10.2018 13:32",
          "msdyn_starttime": "2018-10-29T13:32:00Z",
          "msdyn_endtime@OData.Community.Display.V1.FormattedValue": "29.10.2018 14:30",
          "msdyn_endtime": "2018-10-29T14:30:00Z",
          "msdyn_duration@OData.Community.Display.V1.FormattedValue": "58",
          "msdyn_duration": 58,
          "foc_sapstatisticalkeyfigurecod": "",
          "statecode@OData.Community.Display.V1.FormattedValue": "Active",
          "statecode": 0,
          "_msdyn_booking_value@OData.Community.Display.V1.FormattedValue": "Wartung 2011153  - Wartung - 181 L1205 Stuttgarter Str.\/Aichbr\u00fccke ",
          "_msdyn_booking_value": "bd90b7e6-18dc-e811-a95a-000d3a29f080",
          "msdyn_bookingjournalid": "d52eafbd-42dc-e811-a95a-000d3a29f080"
        }
      ],
      "msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking@odata.nextLink": "https:\/\/xxx.api.crm4.dynamics.com\/api\/data\/v9.0\/bookableresourcebookings(bd90b7e6-18dc-e811-a95a-000d3a29f080)\/msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking?$select=msdyn_journaltype,msdyn_name,msdyn_starttime,msdyn_endtime,msdyn_duration,foc_sapstatisticalkeyfigurecod,statecode"
    }
  ],
  "msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking@odata.nextLink": "https:\/\/xxx.api.crm4.dynamics.com\/api\/data\/v9.0\/bookableresourcebookings(3931de11-f86d-41a8-b483-c9416d20bf0c)\/msdyn_bookableresourcebooking_msdyn_bookingjournal_Booking?$select=msdyn_journaltype,msdyn_name,msdyn_starttime,msdyn_endtime,msdyn_duration,foc_sapstatisticalkeyfigurecod,statecode"
}

1 个答案:

答案 0 :(得分:1)

查看您的数据,假设您没有@作为域名的电子邮件,我认为您可以替换所有OData之后的OData。因此,您可以使用此正则表达式,

(?i)@(?=OData)

此处(?i)确保匹配不区分大小写,并且(?=OData)积极向前看确保@之后紧跟OData

并将其替换为空字符串。

Demo

让我知道这是否适合您。

编辑:除了@Link之外,还合并@Id@OData,并在全局范围内将\/替换为/(而不仅仅是name属性) ,您可以使用此正则表达式

(?i)@(?=(?:OData|link|id)\b)|\\(\/)

并根据您的语言将其替换为$1\1

Check this demo

如果您不想将\/全局替换为/,可以使用此正则表达式,

(?i)@(?=(?:OData|link|id)\b)

并用空字符串替换

就像我在评论中说的那样,要在name属性中将\/替换为/,您将不得不使用一些编程代码。