I have the following string :
name: "gcr.io/myproject/github.com/project/project-1:9246b98256013b49"
digest: "sha256:9a9e3a4fb7072b7"
push_timing {
start_time {
seconds: 1660330436
nanos: 983521156
}
end_time {
seconds: 1660330706
nanos: 296478248
}
}
I just want to get the string after ‘name : ‘ and the first following quote ‘"’. The answer string should be ‘gcr.io/myproject/github.com/project/project-1:9246b98256013b49’
I have been trying to use the following command with no luck yet.
image_name = re.search(r'name : "(.*)"', image_info)
So how can I get a string between two other substrings from a string which includes quotes in python ?
>Solution :
It looks like you’re trying to match an extra space in regex.
This line:
image_name = re.search(r'name : "(.*)"', image_info)
is matching for something that starts with name :, while in your file, it starts with name:. Note the extra space.
An easy fix is to just remove the space.
image_name = re.search(r'name: "(.*)"', image_info)
As @KingsMMA mentioned in the comments, your file seems to be in the form of a JSON file. You can try parsing it as such, which means you can retrieve other elements of your file (like digest) much more easily.