-3

For example,

I have the string 'abdadqdqbdabdabdawb'

How can I find substrings, which start with 'a' and end with 'b'.

The output should be:

['ab', 'adqdqb', 'ab', 'ab', 'awb'] 
bphi
  • 3,115
  • 3
  • 23
  • 36
  • can you show your code ? or you want us to write it ? – Druta Ruslan May 23 '18 at 18:51
  • Well, I am a novice in python, tried billions of alternatives.. So I don't know, which one to post. – Jonas Virkutis May 23 '18 at 18:52
  • 1
    @JonasVirkutis, How about choose the option which you feel is *closest* to a good / correct solution? We usually expect correct syntax, but not necessarily code which works as desired. – jpp May 23 '18 at 18:53
  • string = "abdadqdqbdabdabdawb" list = [] for i in range(0, len(string)): oba.append(string[i:i+5]) print([w for w in list if w.startswith("a")]) – Jonas Virkutis May 23 '18 at 18:58
  • @JonasVirkutis Your example output seems to only include the first matching substring for each "a". Should the output include the _entire_ list of substrings, which would include "abdadqdqbdabdabdawb", for example? – Tim Johns May 23 '18 at 19:01
  • Well, this code creates a list with specific words of length 5 and finds one's, which start with letter a. However, I do not understand what kind of regular expressions should I use to find specific substrings that I have mentioned above. – Jonas Virkutis May 23 '18 at 19:03
  • `re.findall('a.*?b', somestring)` will get all non-overlapping strings. – tdelaney May 23 '18 at 19:03
  • Well, the output should include the list of substrings that start with letter "a" and end with letter "b". For instance, [ab, adqdqb, ... . – Jonas Virkutis May 23 '18 at 19:05
  • Is there a possibility to find overlapping strings? – Jonas Virkutis May 23 '18 at 19:57

1 Answers1

0
import re
re.findall(r'[Aa][^Bb]*[Bb]', 'abdadqdqbdabdabdawb')
>>> ['ab', 'adqdqb', 'ab', 'ab', 'awb'] 
bphi
  • 3,115
  • 3
  • 23
  • 36