I'm new to Python and still learning about regular expressions, so this question may sound trivial to some regex expert, but here you go. I suppose my question is a generalization of this question about finding a string between two strings. I wonder: what if this pattern (initial_substring + substring_to_find + end_substring) is repeated many times in a long string? For example
test='someth1 var="this" someth2 var="that" '
result= re.search('var=(.*) ', test)
print result.group(1)
>>> "this" someth2 var="that"
Instead, I'd like to get a list like ["this","that"]
.
How can I do it?
Using index() + loop to extract string between two substrings. In this, we get the indices of both the substrings using index(), then a loop is used to iterate within the index to find the required string between them.
Extract substring between two markers using split() method Next method that we will be using is the split() method of Python Programming language, to extract a given substring between two markers. The split() method in python splits the given string from a given separator and returns a list of splited substrings.
The simplest way to extract the string between two parentheses is to use slicing and string. find() . First, find the indices of the first occurrences of the opening and closing parentheses. Second, use them as slice indices to get the substring between those indices like so: s[s.
Use re.findall()
:
result = re.findall(r'var="(.*?)"', test)
print(result) # ['this', 'that']
If the test
string contains multiple lines, use the re.DOTALL
flag.
re.findall(r'var="(.*?)"', test, re.DOTALL)
The problem with your current regex
is that the capture group (.*)
is an extremely greedy statement. After the first instance of a var=
in your string, that capture group will get everything after it.
If you instead decrease the generalization of the expression to var="(\w+)"
, you will not have the same issue, therefore changing that line of python
to:
result = re.findall(r'var="([\w\s]+)"', test)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With