- What are Java Regex?
- What are the type of classes in Java?
- What is Matcher class?
- What is an example of Java Regular Expressions?
- Write a regex to split String by new line?
- How can we split Java String by newlines?
- What is a metacharacter?
- What is Flags?
S3 – Regex Interview Questions | Part 1 – Easy
The + operator matches one or more occurrences of the character or group that it precedes, while the * operator matches zero or more occurrences of the character or group that it precedes. So, for example, if you have the regular expression “a+b*c”, it will match any string that starts with an “a” and then has any number of b’s (including zero) before ending with a “c”.
Backreferences are a way of referring back to a previously matched group within a regular expression. This can be useful for matching patterns that repeat, or for extracting information from a string. For example, if you are trying to match a phone number in a string, you could use a backreference to match the area code.
Raw strings are used when you need to match a regular expression pattern exactly, without any special characters being interpreted as metacharacters. This is often the case when working with backslashes, which are used as escape characters in many programming languages. By using a raw string, you can ensure that the backslashes are not interpreted as escape characters, but are instead matched as part of the regular expression pattern.
Greedy matching is a type of matching that occurs in regular expressions when the quantifier (the symbol that indicates how many times a character can occur) is set to match the maximum number of characters possible. For example, the regular expression .* will match any character, including spaces, zero or more times. This means that it will match as much of the string as possible.
Regular expressions are a powerful tool for matching patterns in strings. In Python, regular expressions are handled by the re module. The re module provides a set of functions that allow you to search for patterns in strings, replace those patterns with other strings, and split strings into a list of substrings.
Question #1 – Disemvowel Trolls
Trolls are attacking your comment section!
A common way to deal with this situation is to remove all of the vowels from the trolls’ comments, neutralizing the threat.
Your task is to write a function that takes a string argument and returns a new string with all vowels removed.
For example, the string “Hello World!” would become “Hll Wrld”.
Note: For this problem, ‘y’ is NOT considered a vowel.
This doesn’t look too bad, does it? Let’s jump right in!
Here is what the skeleton of the solution looks like:
When I first began coding, my instinct for this solution would have been to create a temporary string, loop through the input string, and concatenate every letter that isn’t a vowel onto the temp string before returning it. However, programmers are smart folk – I’ll bet you can already tell me that this isn’t the most efficient solution.
We can use a Regular Expression (RegEx) to greatly reduce the complexity of the solution. Our goal is to replace every vowel (lower and uppercase) with a blank space. We can use the regular expression ‘substitute‘ operation to replace all the vowels with a blank space.
The substitute operation has 3 arguments we’ll be using:
In python, RegEx uses the “re” class, so let’s be sure to import that into our code:
Output:
The VOWELS_REGEX variable is, of course, used to find the vowels in the string. To make a regex pattern, we start the string with an ‘r’ to tell python to create a “raw string pattern”. This lets us use escape characters, or “”, in our patterns followed by regex special characters.
Next, we use a regex metacharacter (a character with a special meaning). In this case, we use the metacharacter “[]”, which contains a set of characters that will match any character inside of it. For example, “[0-9a-zA-Z]” means it will match all numbers from 0 to 9 and all lowercase and uppercase letters from a-z. I’m sure you noticed using ranges in a set decreased the number of characters you need to type out.
The code works! Great job
Question #2 – Find Valid Phone Number
Write a function that accepts a string and searches it for a valid phone number.
Return the phone number if found.
A valid phone number may be one of the following:
Here is what the skeleton of the solution looks like:
To solve this problem, we’ll be using the regular expression operation, “search” to find the pattern (phone number) in the text.
The search operation has 2 arguments we’ll be using:
Here is the solution:
Output:
The PHONE_REGEX variable is used to find the phone number. Again, we start it with ‘r’ to signify the creation of a raw string.
To start with, we use a parenthesis (which is the beginning of a group in Regex. I’ll explain why in a minute.
Then, we use a ‘’, which is an escape character. It signifies the beginning of a special regex sequence and follows with a character that has a special meaning.
We follow the escape character by a ‘d{3}’, which means ‘match 3 digits’. This will match the first 3 digits of the phone number with the format “xxx”. But what about the other format?
To match the other format, we need to use the pipe (‘|’), because we want to find “xxx” OR “(xxx)”. Be sure to keep both formats inside of the group we created at the beginning of the pattern.
Follow that by the escape character “” and “(“, which looks for literal parentheses. If you don’t use the escape character before the parentheses, it will assume you are starting another group.
Finally, we finish the pattern with a “-” and more “digit” special characters (followed by how many digits to match it to, {3} and {4}).
Remember, the search operator returns a match object. In order to get the phone number out of the match object, access it using the group operator. Pass an int argument to the operator to denote which group to return. You can make new groups by surrounding characters with parentheses inside a pattern. By default, The entire match is the zeroth group, accessed by either “match.group()” or “match.group(0)“.
That’s it! The code works. Do you feel more confident with regex yet? Just in case, let’s do more!
Serious about Learning Scripting ? Learn this and a lot more with
By creating an account I have read and agree to InterviewBit’s Terms and Privacy Policy.
FAQ
What is RegEx used for?
How many types of RegEx are there?
Is RegEx a skill?
What is RegEx method?