Reputation: 393
Suppose I have JSON strings like this:
[
{
"key1": "value",
"key2": {
"word1.word2.word3.word4.interestingPart": {
"key1": "value",
"key2": "value"
}
},
"key3": "value"
},
{
"key1": "value",
"key2": {
"word1.word2.word3.word4.word5.word6.thisIsAlsoAnInterestingPart": {
"key1": "value",
"key2": "value"
}
},
"key3": "value"
}
]
How do I use regex to match that last "interesting part" in a key? I can expect that the key always starts the same way (word1, word2, word3, word4). However, there might be something that I don't want to be included in the match that differs from file to file (word5, word6).
This is what I have come up so far:
.+(\..+)+(?=")
https://regex101.com/r/BjOcfl/1
It matches the last part but also includes one preceding period. I'd like it to match just the word without anything else.
UPDATE: I had used Python regex rules which apparently do not work in my case. Using Python, my regex matches the last part with one additional period but with JavaScript rules it matches the whole key.
https://regex101.com/r/0VRhl5/2
Upvotes: 3
Views: 334
Reputation: 163577
If you can not use a parser, you could match a "
followed by matching any char except "
or a whitespace char using a negated character class
Then let the pattern backtrack to the last dot, and capture the last part in a group until the first occurrence of "
"[^\s"]+\.([^"\s]+)":\s*{
Another option matching only word chars and repeat matching a dot followed by word chars 1 or more times in a non capturing group "\w+(?:\.\w+)+
"\w+(?:\.\w+)+\.(\w+)":\s*{
Upvotes: 0