পাইথনের re মডিউলে re.sub() মেথড থাকে যা একটি প্যাটার্ন সার্চ করে এবং নতুন সাবস্ট্রিং দিয়ে রিপ্লেস করে। যদি প্যাটার্ন না পাওয়া যায়, তবে স্ট্রিং অনির্দিষ্ট রিটার্ন হয়।
re.sub()-এর সিনট্যাক্স:
re.sub(pattern, repl, string)
উদাহরণস্বরূপ, নিচের কোডে আমরা 'India' সার্চ করব এবং 'the World' দিয়ে রিপ্লেস করব স্ট্রিং 'TP is most popular Tutorials site of India'-এ।
উদাহরণ
result = re.sub(r'India', 'the World', 'TP is the most popular Tutorials site of India') print(result)
আউটপুট
TP is the most popular Tutorials site of the World
নোট: re.subn() রিটার্ন করে (new_string, number_of_subs_made)। re.sub()-এ count প্যারামিটার দিয়ে সর্বোচ্চ রিপ্লেসমেন্ট সংখ্যা সীমাবদ্ধ করা যায়।