The data I have in the test.txt file looks like this:
XXTYSVASOOXOSJAY
CGTVHIXXHIAICXWHAYX
and I'm trying to achieve a pattern like this:
XX-X
XX-X-X
This is what I have so far:
import re
data = open("test.txt", "r")
lines = data.readlines()
result = re.sub(r"[^X]+", r"-", str(lines)).strip("-")
if "X" in result:
print(result)
else:
print("No X found")
This is the result I get, it's a single line: XX-X-XX-X-X
.
How can I do this correctly to get the expected result?