1

I have a string string = "radios label="Does the command/question above meet the Rules to the left?" name="tq_utt_test" validates="required" gold="true" aggregation="agg"" and I want to be able to extract the substring within the "name". So in this case I want to extract "tq_utt_test" because it is the substring inside name.

I've tried regex re.findall('name=(.*)\s', string) which I thought would extract everything after the substring name= and before the first space. But after running that regex, it actually returned "tq_utt_test" validates="required" gold="true". So seems like it's returning everything between name= and the last space, instead of everything between name= and first space.

Is there a way to twist this regex so that it returns everything after name= and before the first space?

Stanleyrr
  • 858
  • 3
  • 12
  • 31
  • 1
    `r"name=(.*?)\s"` but `r'name="[^"]*"'` seems much more robust. If you don't want `name=` use a capturing gorup: `r'name="([^"]*)"'`. – ggorlen Oct 12 '20 at 01:09
  • 1
    @ggorlen, thanks! I tried your suggestion and it works. The question you suggested also solves my problem. – Stanleyrr Oct 12 '20 at 01:58

1 Answers1

1

I will just do re.findall('name=([^ ]*)\s', string)

andondraif
  • 262
  • 1
  • 8