Split on several delimiters

Python Practice › RegEx Programs › Program 11

Split on several delimiters

EasyRegEx Programs · 11 of 20

Write a Python program to split text on commas, semicolons and spaces.

Output
['apple', 'banana', 'cherry', 'date', 'fig']

Approach

  1. re.split(r"[,;\s]+", text).

Solution

Python
import re print(re.split(r"[,;\s]+", "apple, banana;cherry  date,fig"))
Output
['apple', 'banana', 'cherry', 'date', 'fig']

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Remove special characters

Python Practice › RegEx Programs › Program 10

Remove special characters

EasyRegEx Programs · 10 of 20

Write a Python program to remove everything except letters, digits and spaces.

Output
Hello World 2026 SQAtools

Approach

  1. [^A-Za-z0-9 ] matches unwanted characters.

Solution

Python
import re print(re.sub(r"[^A-Za-z0-9 ]", "", "Hello, World! #2026 @SQA_tools"))
Output
Hello World 2026 SQAtools

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Extract URLs

Extract URLs

EasyRegEx Programs · 8 of 20

Write a Python program to extract http and https URLs from text.

Output
['https://docs.python.org/3/', 'https://sqatools.in/python-tutorials/', 'http://example.com']

Approach

  1. https?://\S+ and trim trailing punctuation.

Solution

Python
import re text = "Docs at https://docs.python.org/3/, tutorial https://sqatools.in/python-tutorials/. Old: http://example.com"print([u.rstrip(".,") for u in re.findall(r"https?://\S+", text)])
Output
['https://docs.python.org/3/', 'https://sqatools.in/python-tutorials/', 'http://example.com']

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Collapse extra spaces

Collapse extra spaces

EasyRegEx Programs · 9 of 20

Write a Python program to replace runs of whitespace with a single space.

Output
'Python makes text cleaning easy'

Approach

  1. re.sub(r"\s+", " ", text).strip().

Solution

Python
import re text = "  Python    makes\ttext \n cleaning   easy  "print(repr(re.sub(r"\s+", " ", text).strip()))
Output
'Python makes text cleaning easy'

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Extract hashtags and mentions

Extract hashtags and mentions

EasyRegEx Programs · 7 of 20

Write a Python program to extract hashtags and @mentions from a tweet.

Output
['#python', '#testing']
['@sqatools', '@asha_k']

Approach

  1. #\w+ and @\w+.

Solution

Python
import re tweet = "Loving #python and #testing with @sqatools and @asha_k!"print(re.findall(r"#\w+", tweet))print(re.findall(r"@\w+", tweet))
Output
['#python', '#testing']
['@sqatools', '@asha_k']

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Extract all numbers

Extract all numbers

EasyRegEx Programs · 5 of 20

Write a Python program to extract integers and decimals from text.

Output
[-3.5, 1013.0, 82.4]

Approach

  1. -?\d+(?:\.\d+)?.

Solution

Python
import re text = "Temp -3.5C, pressure 1013 hPa, humidity 82.4%"print([float(x) for x in re.findall(r"-?\d+(?:\.\d+)?", text)])
Output
[-3.5, 1013.0, 82.4]

Line by line

Line 4

(?:...) groups the decimal part without creating a capture, so findall returns whole matches.

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Extract dates

Extract dates

EasyRegEx Programs · 6 of 20

Write a Python program to extract dates in DD-MM-YYYY format and print them as YYYY-MM-DD.

Output
2026-11-15
2026-12-01

Approach

  1. Capture the three parts and reorder them.

Solution

Python
import re text = "Sprint 1 ends 15-11-2026, release on 01-12-2026."for d, m, y in re.findall(r"(\d{2})-(\d{2})-(\d{4})", text):    print(f"{y}-{m}-{d}")
Output
2026-11-15
2026-12-01

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Validate PIN codes

Validate PIN codes

EasyRegEx Programs · 3 of 20

Write a Python program to validate 6-digit Indian PIN codes (not starting with 0).

Output
411001 True
011001 False
41100 False
560034 True

Approach

  1. [1-9]\d{5}.

Solution

Python
import re for pin in ["411001", "011001", "41100", "560034"]:    print(pin, bool(re.fullmatch(r"[1-9]\d{5}", pin)))
Output
411001 True
011001 False
41100 False
560034 True

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Check a strong password

Check a strong password

MediumRegEx Programs · 4 of 20

Write a Python program to check that a password has 8+ characters with upper, lower, digit and symbol.

Output
Test@2026    True
password     False
Short1!      False
NoSymbol123  False

Approach

  1. Look-ahead assertions check each rule without consuming characters.

Solution

Python
import re STRONG = re.compile(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}")for pw in ["Test@2026", "password", "Short1!", "NoSymbol123"]:    print(pw.ljust(12), bool(STRONG.fullmatch(pw)))
Output
Test@2026    True
password     False
Short1!      False
NoSymbol123  False

Line by line

Line 3

Each (?=...) checks that some character of that kind exists; .{8,} enforces the length.

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Validate Indian mobile numbers

Validate Indian mobile numbers

EasyRegEx Programs · 2 of 20

Write a Python program to validate 10-digit Indian mobile numbers with an optional +91.

Output
9876543210       True
+91 9123456789   True
+91-8123456789   True
5123456789       False
98765            False

Approach

  1. (\+91[\s-]?)?[6-9]\d{9}.

Solution

Python
import re for n in ["9876543210", "+91 9123456789", "+91-8123456789", "5123456789", "98765"]:    print(n.ljust(16), bool(re.fullmatch(r"(\+91[\s-]?)?[6-9]\d{9}", n)))
Output
9876543210       True
+91 9123456789   True
+91-8123456789   True
5123456789       False
98765            False

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs