1

I have a large string in the format of the following:

'324/;.ke5 efwef dwe,werwrf <>i want this<> ergy;'56\45,> thu ;lokr<>i want this<> htur ;''\> htur> jur'

i know that i can do something along the lines of:

result= text.partition('<>')[-1].rpartition('<>')[0]

but this will just give me what is in between the first <> and the last <> in the string , how can i loop through the whole string and extract what is in between each respective <> <> tag pair?

2 Answers 2

1

You can use regular expressions and findall():

>>> import re
>>> s = "324/;.ke5 efwef dwe,werwrf <>i want this<> ergy;'56\45,> thu ;lokr<>i want this<> htur ;''\> htur> jur"
>>> re.findall(r"<>(.*?)<>", s)
['i want this', 'i want this']

where (.*?) is a capturing group that would match any characters any number of times in a non-greedy mode.

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

2 Comments

Hi i tired using your method and it worked at first but then i tried using it to find everything within a '\/\/' tag and i stopped working, do you know why this is? @alecxe
@abcla I think this can and should qualify as a separate question. Consider posting if you need help - make sure to provide all the details. To close this topic, consider accepting the answer, thanks.
0

I think string.split() is what you want:

>>> text = """'324/;.ke5 efwef dwe,werwrf <>i want this<> ergy;'56\45,> thu ;lokr<>i want this<> htur ;''\> htur> jur'"""
>>> print text.split('<>')[1:-1]
['i want this', " ergy;'56%,> thu ;lokr", 'i want this']

The split() method gives you a list of strings where the argument is used as the delimiter. (https://docs.python.org/2/library/string.html#string.split) Then, [1:-1] gives you a slice of the list without the first and last elements.

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.