import re
regex = re.compile(r"(<img.*?)(src=\")(.*?\/)([^\/]*\")(.*?)(alt=\")(.*?\")([^>]*>)")
test_str = "<img class=\"alignnone size-medium wp-image-18\" src=\"http://localhost/wp-content/uploads/2015/02/300x149-jquerymobile.jpg\" alt=\"\" width=\"300\" height=\"149\">"
subst = "src=\"\" data-path=\"\\1\" data-img=\"\\2\""
result = regex.sub(subst, test_str, count=1)
if result:
print(result)
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