What if you could find any pattern in text with just a simple, neat code snippet?
Why Regex literal syntax (/pattern/) in Ruby? - Purpose & Use Cases
Imagine you need to find all phone numbers in a long text by checking each character one by one manually.
Doing this by hand is slow and tiring. You might miss some numbers or make mistakes because the rules are tricky.
Regex literal syntax lets you write a pattern once and use it easily to find matches quickly and correctly.
text.include?("123-456-7890") # checks only one number
/\d{3}-\d{3}-\d{4}/.match?(text) # finds any phone number patternYou can quickly search, check, or change text patterns without writing long code.
Checking if a user entered a valid email or phone number in a form becomes simple and reliable.
Manual text checks are slow and error-prone.
Regex literals let you write patterns clearly and reuse them easily.
This saves time and reduces mistakes when working with text.
Practice
cat?Solution
Step 1: Understand regex literal syntax
In Ruby, regex literals are written between slashes, like/pattern/.Step 2: Identify the correct pattern for 'cat'
The pattern to match the word 'cat' is simply/cat/.Final Answer:
/cat/ -> Option DQuick Check:
Regex literal = /pattern/ [OK]
- Using quotes instead of slashes
- Calling a non-existent regex() function
- Confusing string and regex syntax
Dog case-insensitively in Ruby?Solution
Step 1: Identify the case-insensitive option
In Ruby regex, theioption after the slashes makes matching case-insensitive.Step 2: Check the options given
Only/Dog/iuses the correctiflag for case-insensitive matching.Final Answer:
/Dog/i -> Option CQuick Check:
Case-insensitive flag = i [OK]
- Using g or m flags which mean different things
- Omitting the i flag for case-insensitivity
- Placing flags inside the slashes
text = "Hello World" match = text =~ /world/i puts match
Solution
Step 1: Understand the =~ operator with regex
The operator=~returns the index of the first match or nil if no match.Step 2: Analyze the regex and string
The regex/world/imatches 'World' ignoring case. In "Hello World", 'World' starts at index 6 (0-based).Final Answer:
6 -> Option BQuick Check:
Case-insensitive match index = 6 [OK]
- Ignoring case-insensitive flag i
- Thinking output is true/false instead of index
- Assuming match starts at 0 always
pattern = /hello/i/
Solution
Step 1: Check regex literal syntax
A regex literal starts and ends with a single slash. Here, there is an extra slash at the end.Step 2: Identify the syntax error
The extra slash after/hello/iis invalid and causes a syntax error.Final Answer:
Extra slash at the end causes syntax error -> Option AQuick Check:
Regex literal must start and end with one slash pair [OK]
- Adding extra slash after regex
- Confusing regex with string quotes
- Thinking flags go inside slashes
a or A in a string using a regex literal. Which regex literal correctly matches this requirement?Solution
Step 1: Understand the requirement
We want words starting with 'a' or 'A'. Using case-insensitive flagisimplifies matching both.Step 2: Analyze options
/\b[a]\w*/i uses/\b[a]\w*/iwhich matches word boundary, then 'a' (case-insensitive), then zero or more word chars. This matches words starting with 'a' or 'A'.Final Answer:
/\b[a]\w*/i -> Option AQuick Check:
Use i flag to match both 'a' and 'A' easily [OK]
- Listing both 'a' and 'A' inside brackets without i flag
- Using wrong character classes
- Missing word boundary \b
Ready to prove it?
Pass the test and this pattern moves unknown
