xxxxxxxxxx
string_to_test = 'She sells seashells on the seashore'
# let's say you want to find all words containing 'sea' in it
import re
# considering you want a case-insensitive search
regex_obj = re.compile('sea\w*',flags=re.I)
# the re.I signifies case-insensitive search
# the \w signifies any letters, numbers or underscore,
# so in this case, it means any letter after the string 'sea'
# the * signifies one or more occurrences, thus taken together,
# 'sea\w*' signifies one or more instances of any letter,
# number or underscore
list_of_matches = regex_obj.findall(string = string_to_test)
# returns ['seashells','seashore']
xxxxxxxxxx
regexp = re.compile("name(.*)$")
print regexp.search(s).group(1)
# prints " is ryan, and i am new to python and would like to learn more"
xxxxxxxxxx
# credit to Stack Overflow user in the source link
# finds isolated alphabetical characters
import re
s = "fish oil B stack peanut c <b>"
words = re.finditer('\S+', s)
has_alpha = re.compile(r'[a-zA-Z]').search
for word in words:
if len(word.group()) == 1 and has_alpha(word.group()):
print(word.start()) # prints the index inside the string
xxxxxxxxxx
#import the module
import re
word = "valuable"
#text to be scanned
text = "Eric has proved himself to be a valuable asset to the team."
match = re.search(word, text)
s = match.start() #The starting index
e = match.end() #The ending index
print("""Found "%s"
in: %s
from index %d to index %d("%s")."""%(match.re.pattern, match.string, s, e, text[s:e]))
xxxxxxxxxx
document
.getElementById('convertBtn')
.addEventListener('click', function () {
// Create SVG dynamically
const svgString = `
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="80" fill="orange" />
<text x="100" y="115" font-size="20" text-anchor="middle" fill="white">Hello!</text>
</svg>
`;
// Create a Blob from the SVG string
const svgBlob = new Blob([svgString], {
type: 'image/svg+xml;charset=utf-8',
});
const url = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = function () {
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
URL.revokeObjectURL(url); // Clean up
};
img.src = url;
});