Initial commit
This commit is contained in:
3
python/Regular_Expression/Abbreviate 'Road' as 'Rd.py
Normal file
3
python/Regular_Expression/Abbreviate 'Road' as 'Rd.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
street = '21 Ramkrishna Road'
|
||||
print(re.sub('Road$', 'Rd.', street))
|
||||
@@ -0,0 +1,14 @@
|
||||
def is_decimal(num):
|
||||
import re
|
||||
dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")
|
||||
result = dnumre.search(num)
|
||||
return bool(result)
|
||||
|
||||
print(is_decimal('123.11'))
|
||||
print(is_decimal('123.1'))
|
||||
print(is_decimal('123'))
|
||||
print(is_decimal('0.21'))
|
||||
|
||||
print(is_decimal('123.1214'))
|
||||
print(is_decimal('3.124587'))
|
||||
print(is_decimal('e666.86'))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def end_num(string):
|
||||
text = re.compile(r".*[0-9]$")
|
||||
if text.match(string):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
print(end_num('abcdef'))
|
||||
print(end_num('abcdef6'))
|
||||
@@ -0,0 +1,8 @@
|
||||
import re
|
||||
def is_allowed_specific_char(string):
|
||||
charRe = re.compile(r'[^a-zA-Z0-9.]')
|
||||
string = charRe.search(string)
|
||||
return not bool(string)
|
||||
|
||||
print(is_allowed_specific_char("ABCDEFabcdef123450"))
|
||||
print(is_allowed_specific_char("*&%@#!}{"))
|
||||
@@ -0,0 +1,6 @@
|
||||
import re
|
||||
def change_date_format(dt):
|
||||
return re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})', '\\3-\\2-\\1', dt)
|
||||
dt1 = "2026-01-02"
|
||||
print("Original date in YYY-MM-DD Format: ",dt1)
|
||||
print("New date in DD-MM-YYYY Format: ",change_date_format(dt1))
|
||||
@@ -0,0 +1,6 @@
|
||||
def camel_to_snake(text):
|
||||
import re
|
||||
str1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
|
||||
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', str1).lower()
|
||||
|
||||
print(camel_to_snake('PythonExercises'))
|
||||
@@ -0,0 +1,5 @@
|
||||
def snake_to_camel(word):
|
||||
import re
|
||||
return ''.join(x.capitalize() or '_' for x in word.split('_'))
|
||||
|
||||
print(snake_to_camel('python_exercises'))
|
||||
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
text = "PHP Exercises"
|
||||
print("Original Text: ",text)
|
||||
redata = re.compile(re.escape('php'), re.IGNORECASE)
|
||||
new_text = redata.sub('Python', 'PHP Exercises')
|
||||
print("Using 'php' replace PHP")
|
||||
print("New Text: ",new_text)
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text1 = '"Python", "PHP", "Java"'
|
||||
print(re.findall(r'"(.*?)"', text1))
|
||||
@@ -0,0 +1,5 @@
|
||||
import re
|
||||
def extract_date(url):
|
||||
return re.findall(r'/(\d{4})/(\d{1,2})/(\d{1,2})/', url)
|
||||
url1= "https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/"
|
||||
print(extract_date(url1))
|
||||
@@ -0,0 +1,4 @@
|
||||
import re
|
||||
text = "Clearly, he has no excuse for such behavior."
|
||||
for m in re.finditer(r"\w+ly", text):
|
||||
print('%d-%d: %s' % (m.start(), m.end(), m.group(0)))
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = 'The quick brown fox jumps over the lazy dog.'
|
||||
print(re.findall(r"\b\w{5}\b", text))
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = 'The quick brown fox jumps over the lazy dog.'
|
||||
print(re.findall(r"\b\w{3,5}\b", text))
|
||||
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
# Input.
|
||||
text = "The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly."
|
||||
#find all the words starting with ‘a’ or ‘e’
|
||||
list = re.findall("[ae]\w+", text)
|
||||
# Print result.
|
||||
print(list)
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = 'The quick brown fox jumps over the lazy dog.'
|
||||
print(re.findall(r"\b\w{4,}\b", text))
|
||||
@@ -0,0 +1,11 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '^[a-z]+_[a-z]+$'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("aab_cbbbc"))
|
||||
print(text_match("aab_Abbbc"))
|
||||
print(text_match("Aaab_abbbc"))
|
||||
@@ -0,0 +1,11 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '^[a-z]+_[a-z]+$'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("aab_cbbbc"))
|
||||
print(text_match("aab_Abbbc"))
|
||||
print(text_match("Aaab_abbbc"))
|
||||
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
text = 'Python exercises, PHP exercises, C# exercises'
|
||||
pattern = 'exercises'
|
||||
for match in re.finditer(pattern, text):
|
||||
s = match.start()
|
||||
e = match.end()
|
||||
print('Found "%s" at %d:%d' % (text[s:e], s, e))
|
||||
@@ -0,0 +1,5 @@
|
||||
import re
|
||||
text = 'Python exercises, PHP exercises, C# exercises'
|
||||
pattern = 'exercises'
|
||||
for match in re.findall(pattern, text):
|
||||
print('Found "%s"' % match)
|
||||
5
python/Regular_Expression/Find urls in a string.py
Normal file
5
python/Regular_Expression/Find urls in a string.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import re
|
||||
text = '<p>Contents :</p><a href="http://w3resource.com">Python Examples</a><a href="http://github.com">Even More Examples</a>'
|
||||
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
|
||||
print("Original string: ",text)
|
||||
print("Urls: ",urls)
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '^[a-zA-Z0-9_]*$'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("The quick brown fox jumps over the lazy dog."))
|
||||
print(text_match("Python_Exercises_1"))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
|
||||
# Sample strings.
|
||||
words = ["Python PHP", "Java JavaScript", "c c++"]
|
||||
|
||||
for w in words:
|
||||
m = re.match("(P\w+)\W(P\w+)", w)
|
||||
# Check for success
|
||||
if m:
|
||||
print(m.groups())
|
||||
@@ -0,0 +1,11 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'a.*?b'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("aabbbbc"))
|
||||
print(text_match("aabAbbbc"))
|
||||
print(text_match("ABCDEF"))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'ab+?'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("ab"))
|
||||
print(text_match("abc"))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'ab{3}?'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("abbb"))
|
||||
print(text_match("aabbbbbc"))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'ab{2,3}?'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("ab"))
|
||||
print(text_match("aabbbbbc"))
|
||||
@@ -0,0 +1,11 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'ab*?'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("ac"))
|
||||
print(text_match("abc"))
|
||||
print(text_match("abbc"))
|
||||
@@ -0,0 +1,12 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = 'ab??'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("ab"))
|
||||
print(text_match("abc"))
|
||||
print(text_match("abbc"))
|
||||
print(text_match("aabbc"))
|
||||
@@ -0,0 +1,11 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '\w+\S*$'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("The quick brown fox jumps over the lazy dog."))
|
||||
print(text_match("The quick brown fox jumps over the lazy dog. "))
|
||||
print(text_match("The quick brown fox jumps over the lazy dog "))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '^\w+'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("The quick brown fox jumps over the lazy dog."))
|
||||
print(text_match(" The quick brown fox jumps over the lazy dog."))
|
||||
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '\Bz\B'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("The quick brown fox jumps over the lazy dog."))
|
||||
print(text_match("Python Exercises."))
|
||||
10
python/Regular_Expression/Matches a word containing 'z'.py
Normal file
10
python/Regular_Expression/Matches a word containing 'z'.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import re
|
||||
def text_match(text):
|
||||
patterns = '\w*z.\w*'
|
||||
if re.search(patterns, text):
|
||||
return 'Found a match!'
|
||||
else:
|
||||
return('Not matched!')
|
||||
|
||||
print(text_match("The quick brown fox jumps over the lazy dog."))
|
||||
print(text_match("Python Exercises."))
|
||||
@@ -0,0 +1,4 @@
|
||||
import re
|
||||
text1 = ' Python Exercises '
|
||||
print("Original string:",text1)
|
||||
print("Without extra spaces:",re.sub(r'\s+', '',text1))
|
||||
@@ -0,0 +1,4 @@
|
||||
import re
|
||||
text1 = '**//Python Exercises// - 12. '
|
||||
pattern = re.compile('[\W_]+')
|
||||
print(pattern.sub('', text1))
|
||||
@@ -0,0 +1,4 @@
|
||||
import re
|
||||
ip = "216.08.094.196"
|
||||
string = re.sub('\.[0]*', '.', ip)
|
||||
print(string)
|
||||
@@ -0,0 +1,4 @@
|
||||
import re
|
||||
text1 = 'Python Exercises'
|
||||
print("Original string:",text1)
|
||||
print("Without extra spaces:",re.sub(' +',' ',text1))
|
||||
@@ -0,0 +1,6 @@
|
||||
import re
|
||||
text = "\t\u001b[0;35mgoogle.com\u001b[0m \u001b[0;36m216.58.218.206\u001b[0m"
|
||||
print("Original Text: ",text)
|
||||
reaesc = re.compile(r'\x1b[^m]*m')
|
||||
new_text = reaesc.sub('', text)
|
||||
print("New Text: ",new_text)
|
||||
@@ -0,0 +1,6 @@
|
||||
def is_decimal(num):
|
||||
import re
|
||||
text = "The quick brown fox jumps over the lazy dog."
|
||||
# remove words between 1 and 3
|
||||
shortword = re.compile(r'\W*\b\w{1,3}\b')
|
||||
print(shortword.sub('', text))
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = 'Python Exercises, PHP exercises.'
|
||||
print(re.sub("[ ,.]", ":", text))
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = 'Python Exercises, PHP exercises.'
|
||||
print(re.sub("[ ,.]", ":", text, 2))
|
||||
@@ -0,0 +1,6 @@
|
||||
import re
|
||||
text = 'Python Exercises'
|
||||
text =text.replace (" ", "_")
|
||||
print(text)
|
||||
text =text.replace ("_", " ")
|
||||
print(text)
|
||||
@@ -0,0 +1,8 @@
|
||||
import re
|
||||
pattern = 'fox'
|
||||
text = 'The quick brown fox jumps over the lazy dog.'
|
||||
match = re.search(pattern, text)
|
||||
s = match.start()
|
||||
e = match.end()
|
||||
print('Found "%s" in "%s" from %d to %d ' % \
|
||||
(match.re.pattern, match.string, s, e))
|
||||
@@ -0,0 +1,9 @@
|
||||
import re
|
||||
patterns = [ 'fox', 'dog', 'horse' ]
|
||||
text = 'The quick brown fox jumps over the lazy dog.'
|
||||
for pattern in patterns:
|
||||
print('Searching for "%s" in "%s" ->' % (pattern, text),)
|
||||
if re.search(pattern, text):
|
||||
print('Matched!')
|
||||
else:
|
||||
print('Not Matched!')
|
||||
@@ -0,0 +1,5 @@
|
||||
import re
|
||||
results = re.finditer(r"([0-9]{1,3})", "Exercises number 1, 12, 13, and 345 are important")
|
||||
print("Number of length 1 to 3")
|
||||
for n in results:
|
||||
print(n.group(0))
|
||||
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
# Input.
|
||||
text = "The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly."
|
||||
|
||||
for m in re.finditer("\d+", text):
|
||||
print(m.group(0))
|
||||
print("Index position:", m.start())
|
||||
@@ -0,0 +1,7 @@
|
||||
import re
|
||||
# Sample string.
|
||||
text = "Ten 10, Twenty 20, Thirty 30"
|
||||
result = re.split("\D+", text)
|
||||
# Print results.
|
||||
for element in result:
|
||||
print(element)
|
||||
@@ -0,0 +1,3 @@
|
||||
import re
|
||||
text = "PythonTutorialAndExercises"
|
||||
print(re.findall('[A-Z][^A-Z]*', text))
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
# A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.
|
||||
|
||||
import re
|
||||
text = 'The quick brown\nfox jumps*over the lazy dog.'
|
||||
print(re.split('; |, |\*|\n',text))
|
||||
@@ -0,0 +1,9 @@
|
||||
import re
|
||||
def match_num(string):
|
||||
text = re.compile(r"^5")
|
||||
if text.match(string):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
print(match_num('5-2345861'))
|
||||
print(match_num('6-2345861'))
|
||||
Reference in New Issue
Block a user