Bird
Raised Fist0
NLPml~20 mins

Regular expressions for text cleaning in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Regex Text Cleaning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this regex substitution?
Given the text "Hello!!! Are you #1?", what is the result after applying re.sub(r'[^a-zA-Z0-9 ]', '', text)?
NLP
import re
text = "Hello!!! Are you #1?"
result = re.sub(r'[^a-zA-Z0-9 ]', '', text)
print(result)
AHello Are you 1?
BHello!!! Are you 1
CHello Are you #1
DHello Are you 1
Attempts:
2 left
💡 Hint
The regex removes all characters except letters, digits, and spaces.
Model Choice
intermediate
1:30remaining
Which regex pattern removes all digits from a string?
You want to remove all digits from a text string using re.sub. Which pattern should you use?
Ar'\d+'
Br'\D+'
Cr'\w+'
Dr'\s+'
Attempts:
2 left
💡 Hint
Digits are represented by \d in regex.
Metrics
advanced
2:00remaining
How many tokens remain after cleaning?
Given the text "Data science 101: Clean, analyze, & visualize!", after applying re.sub(r'[^a-zA-Z ]', '', text).lower().split(), how many tokens are in the resulting list?
NLP
import re
text = "Data science 101: Clean, analyze, & visualize!"
cleaned = re.sub(r'[^a-zA-Z ]', '', text).lower().split()
print(len(cleaned))
A7
B5
C6
D4
Attempts:
2 left
💡 Hint
Digits and punctuation are removed before splitting by spaces.
🔧 Debug
advanced
2:30remaining
Why does this regex fail to remove punctuation?
This code aims to remove punctuation but does not work as expected:
import re
text = "Hello, world!"
cleaned = re.sub(r'[\w]', '', text)
print(cleaned)
Why?
NLP
import re
text = "Hello, world!"
cleaned = re.sub(r'[\w]', '', text)
print(cleaned)
AThe pattern '[\w]' matches letters and digits, so it removes them instead of punctuation.
BThe pattern '[\w]' matches punctuation only, so letters remain.
CThe pattern is missing a quantifier like '+' to match multiple characters.
DThe pattern should be '[^\w]' to remove punctuation.
Attempts:
2 left
💡 Hint
Check what \w matches in regex.
🧠 Conceptual
expert
3:00remaining
Which regex pattern best cleans URLs from text?
You want to remove URLs from text data using regex. Which pattern is most effective?
Ar'http://'
Br'www\.\w+'
Cr'https?://\S+'
Dr'\S+\.com'
Attempts:
2 left
💡 Hint
URLs often start with http or https and continue until a space.

Practice

(1/5)
1. What is the main purpose of using regular expressions in text cleaning for NLP?
easy
A. To find and remove unwanted patterns or characters in text
B. To train machine learning models directly
C. To store large datasets efficiently
D. To visualize text data with graphs

Solution

  1. Step 1: Understand the role of regular expressions

    Regular expressions are used to identify patterns in text, such as unwanted characters or specific sequences.
  2. Step 2: Connect to text cleaning

    Text cleaning involves removing or replacing unwanted parts of text to prepare it for analysis or modeling.
  3. Final Answer:

    To find and remove unwanted patterns or characters in text -> Option A
  4. Quick Check:

    Regular expressions clean text by pattern matching [OK]
Hint: Regular expressions = pattern search and replace in text [OK]
Common Mistakes:
  • Confusing regex with model training
  • Thinking regex stores data
  • Assuming regex creates visualizations
2. Which of the following is the correct Python syntax to import the regular expression module?
easy
A. from regex import *
B. import regex
C. import re
D. import regular_expression

Solution

  1. Step 1: Recall Python's regex module name

    Python's built-in module for regular expressions is named 're'.
  2. Step 2: Check syntax correctness

    The correct import statement is 'import re' to use regex functions.
  3. Final Answer:

    import re -> Option C
  4. Quick Check:

    Python regex module = re [OK]
Hint: Remember: Python regex module is 're' not 'regex' [OK]
Common Mistakes:
  • Using 'import regex' which is not standard
  • Trying to import non-existent modules
  • Confusing module names with function names
3. What will be the output of this Python code snippet?
import re
text = "Hello, World! 123"
cleaned = re.sub(r'[^a-zA-Z ]', '', text)
print(cleaned)
medium
A. Hello World
B. Hello World 123
C. Hello, World!
D. HelloWorld123

Solution

  1. Step 1: Understand the regex pattern used

    The pattern '[^a-zA-Z ]' means any character NOT a letter (a-z or A-Z) or space.
  2. Step 2: Apply re.sub to remove unwanted characters

    All characters except letters and spaces are removed, so commas, exclamation marks, and digits are deleted.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    Regex removes non-letters/spaces = 'Hello World ' [OK]
Hint: [^...] means NOT those characters, so it removes digits and punctuation [OK]
Common Mistakes:
  • Thinking digits remain after substitution
  • Confusing character classes with ranges
  • Ignoring spaces in the pattern
4. Identify the error in this regex code snippet for removing digits from text:
import re
text = "Price: 100 dollars"
cleaned = re.sub(r'\d', '', text)
print(cleaned)
medium
A. The pattern '\d' should be '\D' to remove digits
B. The backslash in '\d' is not escaped properly
C. The re.sub function is used incorrectly
D. The code will run correctly and remove digits

Solution

  1. Step 1: Check regex pattern correctness

    The pattern r'\d' correctly matches digits (0-9).
  2. Step 2: Verify code syntax and function usage

    The code uses raw string r'\d' which properly escapes the backslash, so digits are removed as intended.
  3. Final Answer:

    The code will run correctly and remove digits -> Option D
  4. Quick Check:

    r'\d' matches digits; re.sub removes them correctly [OK]
Hint: In raw strings, r'\d' matches digits; no extra escaping needed [OK]
Common Mistakes:
  • Thinking '\d' needs double escaping outside raw strings
  • Confusing '\d' with '\D' (non-digit)
  • Assuming re.sub syntax is wrong
5. You want to clean a text dataset by removing all URLs and extra spaces. Which regex pattern and code snippet correctly achieves this in Python?
import re
text = "Visit https://example.com now!  Enjoy!"
cleaned = re.sub(_____, ' ', text)
cleaned = re.sub(r'\s+', ' ', cleaned).strip()
print(cleaned)
hard
A. r'http://[a-z]+'
B. r'https?://\S+'
C. r'www\.[a-z]+\.com'
D. r'https?://[a-z]+'

Solution

  1. Step 1: Identify a regex pattern that matches URLs

    The pattern 'https?://' matches 'http://' or 'https://', and '\S+' matches non-space characters following it, capturing full URLs.
  2. Step 2: Understand the code's cleaning steps

    First, URLs are replaced by a space, then multiple spaces are reduced to one, and leading/trailing spaces removed.
  3. Final Answer:

    r'https?://\S+' -> Option B
  4. Quick Check:

    Use 'https?://\S+' to remove URLs effectively [OK]
Hint: Use 'https?://' plus '\S+' to match full URLs [OK]
Common Mistakes:
  • Using too narrow patterns missing https or full URL
  • Not removing extra spaces after substitution
  • Using patterns that match only partial URLs