Jovian
⭐️
Sign In
In [ ]:
import re

Q1.

Write a regular expression to match all the files that have either .exe, .xml or .jar extensions. A valid file name can contain any alphabet, digit and underscore followed by the extension.

In [ ]:
files = ['employees.xml', 'calculator.jar', 'nfsmw.exe', 'bkgrnd001.jpg', 'sales_report.ppt']

result = []

# write your code here

# print result - result should only contain the items that match the pattern. In this case, result should be ['employees.xml', 'calculator.jar', 'nfsmw.exe']
print(result)

Q2.

Write a regular expression to match all the addresses that have Koramangala embedded in them.

Strings that should match:

  • 466, 5th block, Koramangala, Bangalore
  • 4th BLOCK, KORAMANGALA - 560034

Strings that shouldn't match:

  • 999, St. Marks Road, Bangalore
In [ ]:
addresses = ['466, 5th block, Koramangala, Bangalore', '4th BLOCK, KORAMANGALA - 560034', '999, St. Marks Road, Bangalore']

result = []

# write your code here


# print result - result should only contain the items that match the pattern
print(result)

Q3.

Write a regular expression that matches either integer numbers or floats upto 2 decimal places.

Strings that should match:

  • 2
  • 2.3
  • 4.56
  • .61

Strings that shoudln't match:

  • 4.567
  • 75.8792
  • abc
In [ ]:
numbers = ['2', '2.3', '4.56', '.61', '4.567', '75.8792', 'abc']

result = []

# write your code here


# print result - result should only contain the items that match the pattern
print(result)

Q4.

Write a regular expression to match the model names of smartphones which follow the following pattern:

mobile company name followed by underscore followed by model name followed by underscore followed by model number

Strings that should match:

  • apple_iphone_6
  • samsung_note_4
  • google_pixel_2

Strings that shouldn’t match:

  • apple_6
  • iphone_6
  • google_pixel_
In [ ]:
phones = ['apple_iphone_6', 'samsung_note_4', 'google_pixel_2', 'apple_6', 'iphone_6', 'google_pixel_']

result = []

# write your code here


# print result - result should only contain the items that match the pattern
print(result)

Q5.

Write a regular expression that can be used to match the emails present in a database.

The pattern of a valid email address is defined as follows: The '@' character can be preceded either by alphanumeric characters, period characters or underscore characters. The length of the part that precedes the '@' character should be between 4 to 20 characters.

The '@' character should be followed by a domain name (e.g. gmail.com). The domain name has three parts - a prefix (e.g. 'gmail'), the period character and a suffix (e.g. 'com'). The prefix can have a length between 3 to 15 characters followed by a period character followed by either of these suffixes - 'com', 'in' or 'org'.

Emails that should match:

Emails that shouldn’t match:

In [ ]:
emails = ['random.guy123@gmail.com', 'mr_x_in_bombay@gov.in', '1@ued.org',
          '@gmail.com','abc!@yahoo.in', 'sam_12@gov.us', 'neeraj@']

result = []

# write your code here


# print result - result should only contain the items that match the pattern
print(result)