Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a string between two other substrings from a string which includes quotes in python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading