Regex Case Interview Guide
Regex Case Interview Guide
A regular expression is a special sequence of characters that helps you match or find other strings
or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used
in UNIX world.
The module re provides full support for Perl-like regular expressions in Python. The re module
raises the exception re.error if an error occurs while compiling or using a regular expression.
We would cover two important functions, which would be used to handle regular expressions. But a
small thing first: There are various characters, which would have special meaning when they are
used in regular expression. To avoid any confusion while dealing with regular expressions, we
would use Raw Strings as r'expression'.
Parameter Description
string This is the string, which would be searched to match the pattern at
the beginning of string.
flags You can specify different flags using bitwise OR | . These are
modifiers, which are listed in the table below.
The re.match function returns a match object on success, None on failure. We usegroupnum or
groups function of match object to get matched expression.
Example
#!/usr/bin/python
import re
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"
Parameter Description
string This is the string, which would be searched to match the pattern
anywhere in the string.
flags You can specify different flags using bitwise OR | . These are
modifiers, which are listed in the table below.
The re.search function returns a match object on success, none on failure. We use groupnum or
groups function of match object to get matched expression.
Example
#!/usr/bin/python
import re
if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"
Example
#!/usr/bin/python
import re
No match!!
search --> matchObj.group() : dogs
Syntax
This method replaces all occurrences of the RE pattern in string with repl, substituting all
occurrences unless max provided. This method returns modified string.
Example
#!/usr/bin/python
import re
Modifier Description
re.M Makes $ match the end of a line (not just the end of the string) and
makes ^ match the start of any line (not just the start of the string).
re.U Interprets letters according to the Unicode character set. This flag
affects the behavior of \w, \W, \b, \B.
Following table lists the regular expression syntax that is available in Python −
Pattern Description
a| b Matches either a or b.
?#... Comment.
\S Matches nonwhitespace.
\D Matches nondigits.
Literal characters
Example Description
Character classes
Example Description
Example Description
Repetition Cases
Example Description
Nongreedy repetition
This matches the smallest number of repetitions −
Example Description
Example Description
Backreferences
This matches a previously matched group again −
Example Description
Alternatives
Example Description
Anchors
This needs to specify match position.
Example Description
\brub\B \B is nonword boundary: match "rub" in "rube" and "ruby" but not alone
Note: Frameworks may or may not be appropriate for the case you are given. Be careful not to force a
framework onto the problem. However, a general understanding of the various frameworks will help you
address appropriate issues within the case.
2. Some questions require you to make assumptions. The idea is not that you have the right answer to a question
(for example: How many romance novels are sold each year in Canada?) it is your thought process they want to
see. You should also be familiar with:
3. Research and practice case interviewing in order to become more familiar with the process. Case examples are
available at various consulting firm websites:
www.mcgill.ca/caps Page |1
The Boston Consulting Group
Practice case interviews
https://www.bcg.com/careers/path/consulting/practice-interview-cases.aspx
Interactive case library
https://www.bcg.com/Interactives/ICL/
Oliver Wyman
Case interview tips & simulations
http://www.oliverwyman.com/careers/entry-level/interview-preparation.html
Deloitte
Case interview tips
https://www2.deloitte.com/us/en/pages/careers/articles/join-deloitte-careers-case-interview-
tips.html
Preparing for the case interview
https://www2.deloitte.com/us/en/pages/careers/articles/join-deloitte-case-interview-prep-
tool.html
5. Brush up on your math skills. Errors most commonly occur with the zeros. 100,000 turns into 10,000 or
1,000,000.
www.mcgill.ca/caps Page |2