1

I have this string:

"<C (programming language)> <C++ (programming language)> <Programming Languages> <Computer Programming> "

And i want to obtain a list of substrings, like this:

['<C (programming language)>','<C++ (programming language)>','<Programming Languages>','<Computer Programming>']

I tried to use re library python but without success

3
  • What regular expressions have you tried so far? Are you able to extract at least one substring, e.g. just the first: "<C (programming language)>"? Commented Nov 11, 2015 at 20:35
  • I have tried re.search or re.match for find all substrings with this feature <sub-string> So i use this pattern <.*?> but i have no match. Commented Nov 11, 2015 at 20:47
  • <.*?> should have worked, as you have correctly used the non-greedy *?. Try that with the answer you picked. But this greedy * vs. non-greedy *? is always confusing, so I'd also go with the much clearer <[^>]*> or <[^>]+>. Commented Nov 11, 2015 at 21:50

2 Answers 2

6

Using regular expressions, you can use:

import re
regexp = re.compile("<[^>]+>")
matches = regexp.findall(my_string)

The regular expression basically matches everything starting with a '<' and ending with a '>'. findall then returns all found matches.

Sign up to request clarification or add additional context in comments.

Comments

1

This can be done using the re import, although another solution would be to use the split method as shown here:

st = st.split('>')  # splits the string to a list made of elements divided by the '>' sign but deletes the '>' sign
del st[len(st) - 1]  # Splitting your String like we did will add another unneccesary element in the end of the list
st = [i + ">" for i in st]  # adds back the '>' sign to the every element of the list

Hope it helped

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.