I want regex that accepts any value in the format
int():str():int
For example, my input could be:- 0:apple4:2347
So, the input should accept a digit from 0-9 followed by a colon, then a string with an integer followed by a colon and a four-digit number. Please tell me how to do it?
CodePudding user response:
You could use this simple pattern:
(\d ):(\w ):(\d )
Explanation:
(\d )
match at least one digit or more(\w )
match at least one word or more ([A-Za-z0-9_]
)
Example code:
import re
pattern = re.compile(r"(\d ):(\w ):(\d )")
text = "hello 0:apple4:2347 world 1234:banana39:4094"
matches = re.findall(pattern, text)
print(matches)
CodePudding user response:
Below is my answer
import re
testString=input("Enter String in format int():str():int");
regex=re.compile(r"^\d :\w :\d $")
if (bool(re.match(regex, testString))):
print("String is :",testString)
else:
print("String is not in format int():str():int")