D
D
Dream2021-09-29 11:45:34
Python
Dream, 2021-09-29 11:45:34

How to find a word in a string by criteria?

There is a line: "new version - version1". How to find the word version1 by: only English letters, by the presence of numbers, by the number of characters - in this case there are 7 of them. And all these 3 conditions are mandatory.

Those. there can be several lines, but a word must be found from them according to the criteria
Examples:
"new version yesterday - version1" | Finds: version1
"new version today is version2" | Finds: version2
"new version tomorrow - version3". | Finds: version3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Golovanenko, 2021-09-29
@dreameddd

Use regular expressions:

import re
s = "новая версия вчера - version1"
r = re.search(r'[a-zA-Z]{7}\d', s)
print(r.group())

https://regex101.com/r/tQELMd/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question