WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Basic · question 14 of 100

What are the basic elements of a regular expression in Perl?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

Regular expressions are used in Perl to match patterns in text. The basic elements in a regular expression include:

1. Anchors: Anchors provide a way to match the beginning or end of a string or a line. For example, the ""̂ character at the beginning of a regular expression matches the start of a line, and the "$" character at the end of a regular expression matches the end of a line.

2. Character classes: Character classes are used to match a group of characters. For example, the character class "[aeiou]" matches any vowel.

3. Quantifiers: Quantifiers specify how many times a pattern should be repeated. For example, the quantifier "+" means one or more times, and the quantifier "*" means zero or more times.

4. Grouping: Grouping allows you to group a pattern together so that you can apply a quantifier to it. For example, the pattern "(abc)+" matches one or more occurrences of the sequence "abc".

5. Alternation: Alternation allows you to match one of several patterns. For example, the pattern "apple|orange" matches either "apple" or "orange".

6. Escaping: Escaping is used to match special characters such as ".", "*", and "+". To match these characters literally, you need to escape them with a "c̈haracter.

7. Substitution: Substitution is used in Perl regular expressions to replace matched strings with new strings. The "/s" operator is used to perform substitution.

Here’s an example of a regular expression in Perl that matches a valid email address:

$email = "jane.doe@example.com";
if ($email =~ /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/) {
  print "Valid email address";
}

In this example, the regular expression matches a string that starts with one or more alphanumeric characters, followed by an "@" symbol, followed by one or more alphanumeric characters, a dot, and then two or more letters. The ""̂ anchor matches the start of the string, and the "$" anchor matches the end of the string.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics