import re
regex = re.compile(r"(<dependency>\s+<groupId>)(.+)(</groupId>\s+<artifactId>)(.+)(</artifactId>\s+<version>)([^\$]+?)(</version>[\s\S]*?\s+</dependency>)")
test_str = (" <dependency>\n"
" <groupId>org.example.one</groupId>\n"
" <artifactId>example1</artifactId>\n"
" <version>1.2.3</version>\n"
" </dependency>\n"
" <dependency>\n"
" <groupId>org.example.two</groupId>\n"
" <artifactId>example2</artifactId>\n"
" <version>${example2.version}</version>\n"
" </dependency>\n"
" <dependency>\n"
" <groupId>org.example.three</groupId>\n"
" <artifactId>example3</artifactId>\n"
" <version>1.2.3</version>\n"
" <scope>test</scope>\n"
" </dependency>\n"
" <dependency>\n"
" <groupId>org.example.four</groupId>\n"
" <artifactId>example4</artifactId>\n"
" <version>${example4.version}</version>\n"
" <scope>test</scope>\n"
" </dependency>\n"
" <dependency>\n"
" <groupId>org.example.five</groupId>\n"
" <artifactId>example5</artifactId>\n"
" <version>1.2.3</version>\n"
" <scope>test</scope>\n"
" <exclusions>\n"
" <exclusion>\n"
" <artifactId>*</artifactId>\n"
" <groupId>*</groupId>\n"
" </exclusion>\n"
" </exclusions>\n"
" </dependency>\n"
" <dependency>\n"
" <groupId>org.example.six</groupId>\n"
" <artifactId>example6</artifactId>\n"
" <version>${example6.version}</version>\n"
" <scope>test</scope>\n"
" <exclusions>\n"
" <exclusion>\n"
" <artifactId>*</artifactId>\n"
" <groupId>*</groupId>\n"
" </exclusion>\n"
" </exclusions>\n"
" </dependency>")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html