1

I have a text file with a bunch of random text inside. How would I go about finding the following string in the text file using regex and python.

name: "here's my string"

Basically, I want to find "here's my string" without the quotes if possible using regex and python. I would need to use regex because the string itself and the position of the string could change over time.

Match I'm looking for:

here's my string

I tried:

("name:([^,]+|\S+)"

but I don't get any results back. If someone could help explain where I went wrong that would be very helpful.

Thanks in advance.

Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
Cheese
  • 11
  • 1
  • 2
    why are you using regex? why not just split? ala https://stackoverflow.com/questions/12572362/get-a-string-after-a-specific-substring/12572391#12572391 – Joran Beasley Oct 24 '17 at 17:01
  • This is what your pattern matches: "name" followed by a colon, followed by **either** (one or more characters that are not comma's) **OR** (one or more non-whitespace characters). Using an online regex tester, like https://regex101.com/, makes it easy to test patterns – wwii Oct 24 '17 at 18:50
  • Show us how you use your pattern and how you retreive the match results. – wwii Oct 24 '17 at 18:56
  • i try with your regex and it did works for me, i am using `re.search("name:([^,]+|\S+)", s).group(1)` – Skycc Oct 25 '17 at 01:16

1 Answers1

1

You can try this:

import re
the_string = 'name: "here\'s my string"'
s = re.findall('"(.*?)"', the_string)[0]

Output:

"here's my string"
Ajax1234
  • 69,937
  • 8
  • 61
  • 102