Skip to content

Exercise - Regular Expressions

🔹 Beginner – Literal Matches

Exercise 1

  • Task: Match all occurrences of the word cat.

  • Sample text:

    The cat sat on the catalog next to another cat.


Exercise 2

  • Task: Match the email domain example.com.

  • Sample text:

    Contact me at john@example.com or jane@sample.org.


🔹 Intermediate – Metacharacters & Character Classes

Exercise 3

  • Task: Match any digit.

  • Sample text:

    Order numbers: 123, 456, A789.


Exercise 4

  • Task: Match any word ending with .jpg or .png.

  • Sample text:

    photo.jpg, logo.png, notes.txt


Exercise 5

  • Task: Match any word that starts with a capital letter.

  • Sample text:

    Alice went to Wonderland with Bob and charlie.


🔹 Quantifiers

Exercise 6

  • Task: Match all sequences of 2 to 4 digits.

  • Sample text:

    Codes: 12, 345, 6789, 12345.


Exercise 7

  • Task: Match repeated laughter like ha, haha, hahaha, etc.

  • Sample text:

    He said "ha", then "haha", and finally "hahahaha".


🔹 Groups & Capturing

Exercise 8

  • Task: Match names in "Last, First" format and swap them to "First Last" using search & replace.

  • Sample text:

    Smith, John Doe, Jane


Exercise 9

  • Task: Extract all HTML tags (just the tag name).

  • Sample text:

    <div>Hello</div><span>World</span>


🔹 Advanced – Lookaheads

Exercise 10

  • Task: Match any number that is followed by kg.

  • Sample text:

    50kg 100g 25kg 300ml


Exercise 11

  • Task: Match any word that is not followed by a comma.

  • Sample text:

    Apple, Orange Banana, Grape


Exercise 12

  • Task: Match passwords that contain at least one digit (validation example).

  • Sample text:

    abc123 password hello42