简体   繁体   English

从 Java map 中删除值并使用正则表达式替换为逗号

[英]Remove values from Java map and replace with comma using Regex

I have a regular expression that will target everything to the right of ":" in a map and to the left of a ","我有一个正则表达式,它将针对 map 中“:”右侧和“,”左侧的所有内容

I am using this RegExp:我正在使用这个正则表达式:

:(.*?)(,|})

Like this:像这样:

$PROP_TYPES.replaceAll(":(.*?)(,|})", ",").concat("}")

This works well for the most part, except when the expression runs into objects that are on the right side of the ":"这在大多数情况下都很有效,除非表达式遇到“:”右侧的对象

For example, on a list of prop type definitions in TypeScript:例如,在 TypeScript 中的道具类型定义列表中:

  "open": "boolean",
  "onDidDismiss": "() => void",
  "message": "string",
  "showAddReceiptModal": "boolean",
  "showModal": "(value: (((prevState: boolean) => boolean) | boolean)) => void",
  "vehicleTabRef": "React.MutableRefObject<null>",
  "onReceiptSubmit": "(data) => void",
  "vehicle": "any",
  "showModal1": "boolean",
  "showModal2": "(value: (((prevState: boolean) => boolean) | boolean)) => void",
  "selectedReceipt": "{ __typename: "Receipt" | undefined; id: string | undefined; purchaseDate: string | undefined; sellerName: string | undefined; sellerAddress: string | undefined; sellerCity: string | undefined; sellerState: string | undefined; sellerZipCode: string | undefined; totalAmount: number | undefined; gallonsPurchased: number | undefined; image?: string | null | undefined | undefined; userID?: string | null | undefined | undefined; vehicleID: string | undefined; vehicle?: Vehicle | null | undefined | undefined; taxRefund?: number | null | undefined | undefined; createdAt: string | undefined; updatedAt: string | undefined }",
  "onClick": "() => void",
  "onClick1": "() => void",
  "dataEditing": "boolean",
  "toastMessage": "(val) => void",
  "editing": "(bool) => void",
  "showToast": "(bool) => void",
  "showModal3": "boolean",
  "showModal4": "(bool) => void",
  "dateFilter": "{ startDate: string; endDate: string }",

When I run replaceAll with this regex, and the replacement is a comma I am left with this:当我用这个正则表达式运行 replaceAll 时,替换是一个逗号,我留下了这个:

  open,
  onDidDismiss,
  message,
  showAddReceiptModal,
  showModal,
  vehicleTabRef,
  onReceiptSubmit,
  vehicle,
  showModal1,
  showModal2,
  selectedReceipt,                                                 
  , 
  onClick, 
  onClick1, 
  dataEditing,
  toastMessage, 
  editing, 
  showToast, 
  showModal3, 
  showModal4, 
  dateFilter, 
  , 

Notice the empty commas, these should not be here.注意空逗号,这些不应该在这里。 This expression should target everything to the right of the ":" and replace it all with a comma.此表达式应针对“:”右侧的所有内容并将其全部替换为逗号。

This code is written in VTL to use with the Webstorm IDE to create quick typescript templates.此代码使用 VTL 编写,可与 Webstorm IDE 一起使用,以创建快速 typescript 模板。 But Java string manipulation works as well但是 Java 字符串操作也可以

What regex can I use to only remove everything to the right of the : to the ending comma, and just leave that comma?我可以使用什么正则表达式来仅删除:右侧的所有内容到结尾的逗号,然后留下那个逗号?

You might use:您可能会使用:

^\s*\"([^\"]+)\":.*(?=,)

Explanation解释

  • ^ Start of string ^字符串开头
  • \s* Match optional whitespace chars (Or use \h* in Java to not match newlines) \s*匹配可选的空白字符(或在 Java 中使用\h*以不匹配换行符)
  • \"([^\"]+)\" Capture in group 1 all between double quotes \"([^\"]+)\"在第 1 组中捕获全部在双引号之间
  • :.* Match : and then the rest of the line :.*匹配:然后是 rest 行
  • (?=,) Positive lookahead, assert a comma to the right (?=,)正向前瞻,右置逗号

Regex demo正则表达式演示

In the replacement use capture group 1.在替换使用捕获组 1。

The result after the replacement:替换后的结果:

open,
onDidDismiss,
message,
showAddReceiptModal,
showModal,
vehicleTabRef,
onReceiptSubmit,
vehicle,
showModal1,
showModal2,
selectedReceipt,
onClick,
onClick1,
dataEditing,
toastMessage,
editing,
showToast,
showModal3,
showModal4,
dateFilter,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM