Practice 50 PHP Regular Expression Programming Questions
Q1: Write a regular expression to check if a string contains only digits.
Input: isOnlyDigits("12345")
Expected Output:
True
Q2: Write a regular expression to check if a string contains only alphabets.
Input: isOnlyAlphabets("HelloWorld")
Expected Output:
True
Q3: Write a regular expression to check if a string contains a specific word "PHP".
Input: containsWord("I love PHP")
Expected Output:
True
Q4: Write a regular expression to validate an email address.
Input: validateEmail("example@test.com")
Expected Output:
True
Q5: Write a regular expression to check if a string contains only lowercase letters.
Input: isLowercase("hello")
Expected Output:
True
Q6: Write a regular expression to check if a string starts with a digit.
Input: startsWithDigit("123abc")
Expected Output:
True
Q7: Write a regular expression to validate a phone number (format: 123-456-7890).
Input: validatePhoneNumber("123-456-7890")
Expected Output:
True
Q8: Write a regular expression to find all words in a string.
Input: findWords("Learn PHP with examples")
Expected Output:
["Learn", "PHP", "with", "examples"]
Q9: Write a regular expression to replace all spaces in a string with underscores.
Input: replaceSpaces("PHP is fun")
Expected Output:
PHP_is_fun
Q10: Write a regular expression to extract numbers from a string.
Input: extractNumbers("Order123 contains 45 items")
Expected Output:
[123, 45]
Q11: Write a regular expression to check if a string ends with ".com".
Input: endsWithCom("example.com")
Expected Output:
True
Q12: Write a regular expression to validate a date in the format YYYY-MM-DD.
Input: validateDate("2023-12-29")
Expected Output:
True
Q13: Write a regular expression to count the number of vowels in a string.
Input: countVowels("Hello World")
Expected Output:
3
Q14: Write a regular expression to find all capital letters in a string.
Input: findCapitalLetters("Hello World")
Expected Output:
["H", "W"]
Q15: Write a regular expression to check if a string is a valid hexadecimal color code.
Input: isHexColor("#FF5733")
Expected Output:
True
Q16: Write a regular expression to validate a password with at least 8 characters, one uppercase letter, one lowercase letter, and one digit.
Input: validatePassword("Passw0rd")
Expected Output:
True
Q17: Write a regular expression to split a string by commas.
Input: splitByComma("PHP,Java,Python")
Expected Output:
["PHP", "Java", "Python"]
Q18: Write a regular expression to check if a string is a valid IP address.
Input: isIPAddress("192.168.1.1")
Expected Output:
True
Q19: Write a regular expression to check if a string contains at least one special character.
Input: containsSpecialCharacter("Hello@World")
Expected Output:
True
Q20: Write a regular expression to remove all digits from a string.
Input: removeDigits("PHP123Programming")
Expected Output:
PHPProgramming
Q21: Write a regular expression to find duplicate words in a string.
Input: findDuplicates("This is is a test test")
Expected Output:
["is", "test"]
Q22: Write a regular expression to check if a string contains a valid URL.
Input: isValidURL("https://example.com")
Expected Output:
True
Q23: Write a regular expression to replace multiple spaces in a string with a single space.
Input: replaceMultipleSpaces("This is PHP")
Expected Output:
This is PHP
Q24: Write a regular expression to validate a 5-digit ZIP code.
Input: validateZip("12345")
Expected Output:
True
Q25: Write a regular expression to check if a string contains both uppercase and lowercase letters.
Input: containsBothCases("Hello")
Expected Output:
True
Q26: Write a regular expression to remove all vowels from a string.
Input: removeVowels("Hello World")
Expected Output:
Hll Wrld
Q27: Write a regular expression to validate a time in the format HH:MM (24-hour format).
Input: validateTime("14:30")
Expected Output:
True
Q28: Write a regular expression to find all numeric values in a string.
Input: findNumbers("The price is 45 and 100.")
Expected Output:
[45, 100]
Q29: Write a regular expression to check if a string starts with a specific letter (e.g., 'A').
Input: startsWithA("Apple")
Expected Output:
True
Q30: Write a regular expression to validate a credit card number (16 digits).
Input: validateCard("1234567812345678")
Expected Output:
True
Q31: Write a regular expression to replace all lowercase letters in a string with uppercase letters.
Input: replaceWithUppercase("hello world")
Expected Output:
HELLO WORLD
Q32: Write a regular expression to extract the domain name from an email address.
Input: extractDomain("user@example.com")
Expected Output:
example.com
Q33: Write a regular expression to check if a string contains a valid hashtag (e.g., #example).
Input: isValidHashtag("#LearnPHP")
Expected Output:
True
Q34: Write a regular expression to check if a string contains only alphanumeric characters.
Input: isAlphanumeric("Hello123")
Expected Output:
True
Q35: Write a regular expression to count the number of words starting with 'P' in a string.
Input: countWordsStartingWithP("PHP Programming is Powerful")
Expected Output:
3
Q36: Write a regular expression to extract all email addresses from a string.
Input: extractEmails("Contact us at support@example.com or sales@example.org")
Expected Output:
["support@example.com", "sales@example.org"]
Q37: Write a regular expression to validate a username (only letters, digits, and underscores, 3-15 characters).
Input: validateUsername("user_123")
Expected Output:
True
Q38: Write a regular expression to check if a string contains repeating characters.
Input: hasRepeatingCharacters("book")
Expected Output:
True
Q39: Write a regular expression to replace all HTML tags in a string with an empty string.
Input: removeHTMLTags("<h1>Welcome</h1> to <b>PHP</b>")
Expected Output:
Welcome to PHP
Q40: Write a regular expression to validate a MAC address (e.g., 00:1A:2B:3C:4D:5E).
Input: validateMAC("00:1A:2B:3C:4D:5E")
Expected Output:
True
Q41: Write a regular expression to check if a string is a valid binary number (only 0s and 1s).
Input: isBinary("101010")
Expected Output:
True
Q42: Write a regular expression to extract all uppercase words from a string.
Input: extractUppercaseWords("THIS is A TEST")
Expected Output:
["THIS", "A", "TEST"]
Q43: Write a regular expression to check if a string contains a valid file path (e.g., C:\Program Files).
Input: validateFilePath("C:\\Program Files")
Expected Output:
True
Q44: Write a regular expression to find all substrings enclosed in double quotes.
Input: findQuotedStrings('He said "Hello" and "Goodbye".')
Expected Output:
["Hello", "Goodbye"]
Q45: Write a regular expression to remove all non-alphanumeric characters from a string.
Input: removeNonAlphanumeric("Hello @World!123")
Expected Output:
HelloWorld123
Q46: Write a regular expression to validate a hexadecimal number (e.g., 0x1A3F).
Input: validateHexNumber("0x1A3F")
Expected Output:
True
Q47: Write a regular expression to check if a string contains consecutive repeated characters.
Input: hasConsecutiveRepeats("balloon")
Expected Output:
True
Q48: Write a regular expression to replace all digits in a string with '#'.
Input: replaceDigits("Phone: 123-456-7890")
Expected Output:
Phone: ###-###-####
Q49: Write a regular expression to extract the file extension from a filename.
Input: getFileExtension("document.pdf")
Expected Output:
pdf
Q50: Write a regular expression to validate a US Social Security Number (format: 123-45-6789).
Input: validateSSN("123-45-6789")
Expected Output:
True
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!