我有一个类似JSON的对象:
{
id: "111",
lat: 22.2223,
lng: 4.22354,
city: "CITY",
address: "Address",
postal: "0000 AA",
phone: "000-0000000",
name: "name",
state: "",
country: "Nederland",
fax: "",
email: "",
monday: "Vanaf 1 februari: 11.00 - 17.30",
tuesday: "09:30 - 17:30",
wednesday: "09:30 - 17:30",
thursday: "09:30 - 17:30",
"friday": "09:30 - 20:00",
saturday: "09:30 - 17:00",
sunday: "gesloten",
extra_sundays: "",
"display": "true",
showonmaplink: "true",
appointment_after_6: "false",
url: "https://www.website.com",
additional_info: ""
}
我想找到这个“对象”的所有键,以便在需要时添加引号。现在我使用这个正则表达式:
(['"])?([a-zA-Z_0-9]+)(['"])?:\s
它匹配所有键,但也包含februari
之类的不必要的词
我尝试了很多不同的方法来排除februari,但没有找到正确的解决方案。
请帮忙,如何排除第二次出现\w+: ? \s
我曾经排除“https://www.website.com” - 也许你也可以建议更好的方法
答案 0 :(得分:0)
取决于您的密钥名称的复杂程度,您可以从每行的开头到第一个冒号(:
)进行匹配:
^"?(.+?)"?:
每个令牌的含义:
^ - beginning of line
"? - match the quotation character zero or 1 time
(.+?) - match one or more character, non-greedy (the `?`)
"? - match the quotation character zero or 1 time
: - match the colon character
Stub。如果您的密钥名称包含转义冒号或双引号,则不会起作用,在这种情况下,您需要更复杂的正则表达式模式。