Created
August 4, 2020 03:54
-
-
Save Alfex4936/73ed450b86e45f3d9a1a30a88cf6c354 to your computer and use it in GitHub Desktop.
Python regex examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# result : (010)-XXXX-XXXX | |
data = """ | |
SEEK = 010-1234-5890 | |
TEST = 010-4414-5142 | |
TETT = 011-2421-1242 | |
""" | |
pat = re.compile("(?P<head>\d{3})-(\d{4})-(\d{4})") | |
print(pat.sub("(\g<head>)-\g<2>-\g<3>", data)) | |
# SEEK = (010)-1234-5890 ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def findlongestPalindromicSubstring(string): | |
stack = set() | |
for i, str in enumerate(string): | |
new_string = string[i:] | |
pattern = re.search(str + "(.*)" + str, new_string) | |
if pattern: | |
if is_palindrome(pattern.group()): | |
print('pattern', pattern.group()) | |
stack.add(pattern.group()) | |
return max(stack, key=len) if len(string) > 1 else string | |
def is_palindrome(str): | |
return str == str[::-1] | |
str = "abaabsvssvsaczcxbzccaabaczcaaa" | |
print(findlongestPalindromicSubstring(str)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# ID | |
data = """ | |
KUN : 800905-1049118 | |
SON : 700905-1059119 | |
KIM : 900905 2323222 | |
""" | |
pat = re.compile("(\d{6})[-]\d{7}") | |
print(pat.sub("\g<1>-" + "*" * 7, data)) | |
# KUN : 800905-******* ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment