>
3 bedroom detached bungalow for sale
Ros Lyn, St. Ives
£355,000
Ros Lyn, St. Ives
£355,000
Price History
Initial price | £365,000 |
29/06/24 | £355,000 |
Price Change | -2.74% |
Description
```
I'm trying to use regex to match the text between `<>` and `[INST]` tags. I've tried several regex patterns but I can't seem to get it right. I'm using Python 3.7.
Here's what I've tried so far:
```
import re
text = """
<>
A good sized detached 3 bedroom bungalow with ample off road parking, garage and lovely enclosed rear garden. Situated in a popular residential location within Carbis Bay and being sold with no chain, viewing is recommended<>
"""
# Attempt 1
match_1 = re.search(r'<>(.*?)<>', text)
print(match_1.group(1))
# Attempt 2
match_2 = re.search(r'<>.*?<>', text)
print(match_2.group(0))
# Attempt 3
match_3 = re.search(r'<\>([^\<\<]+)', text)
print(match_3.group(1))
# Attempt 4
match_4 = re.search(r'<>.*?\[\[INST\]\]', text)
print(match_4.group(0))
```
The expected output is:
```
A good sized detached 3 bedroom bungalow with ample off road parking, garage and lovely enclosed rear garden. Situated in a popular residential location within Carbis Bay and being sold with no chain, viewing is recommended
```
However, none of the attempts above are working as expected. The closest I got was with Attempt 4, but it's not capturing the group correctly, it's printing the entire string including `[INST]`.
## Answer (2)
You're close with your attempt 4, but you need to use a non-greedy quantifier `.*?` to match as few characters as possible until the `[INST]` pattern. Also, you should use a capturing group to get the matched text. Here's the corrected regex pattern:
```
import re
text = """