A
A
Artyom Nikiforov2019-09-19 13:47:26
Python
Artyom Nikiforov, 2019-09-19 13:47:26

Is there a function for parsing text according to a template?

For example, there is text:

<тег>1</тег>
<тег>2</тег>
<тег>3</тег>

To parse values ​​between tags, we write something like:
for my_string in text:
    word = Parse('<тег>, my_string, </тег>)
    print(word)

What is the output:
1
2
3

Is there such a function? If not, how to implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2019-09-19
@teacoder

And than the same re does not suit?

import re

str1="""<тег>1</тег><тег>2</тег><тег>3</тег>"""
for res in re.findall(r'<тег>(.*?)<\/тег>', str1):
    print(res)

1
2
3

D
Developer, 2019-09-19
@samodum

This parser is a regular expression.
In python - re library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question