如何用Ruby提取文本文件中以aa或bb开头的单词?
How to Extract Words Starting with "aa" or "bb" from a Text File in Ruby
Hey there! Let's get this sorted for you. Your initial attempts are on the right track, but we need to tweak the regex to properly capture full words instead of just the prefixes or unintended characters.
The Problem with Your Previous Code
- Your first attempt (
/\b#{input}/) only matches the "aa" prefix itself, not the entire word that starts with it. - The second attempt (
/\b#{input}.*?\b/) uses.*?which matches any character (including non-word characters like!or,) until the next word boundary. This could lead to capturing strings you don't want (e.g.,"aa!bb"instead of separate"aa"and"bb"words).
Correct Implementation
Here's how to properly capture all full words starting with either "aa" or "bb":
Basic Approach (Small Files)
filepath = "path/to/your/file.txt" # Scan for full words starting with aa or bb matches = File.read(filepath).scan(/\b(?:aa|bb)\w*\b/) # Print the results to verify puts matches.inspect
Breakdown of the Regex
\b: Ensures we're matching the start of a word (so we don't accidentally capture "xaa123" as part of the match).(?:aa|bb): A non-capturing group that specifies our allowed prefixes. Using?:means we don't get separate results for just "aa" or "bb"—we get the full word.\w*: Matches zero or more word characters (letters, numbers, underscores) to capture the rest of the word. Adjust this to\w+if you want to exclude empty matches (though prefixes like "aa" alone would still count as valid words here).
Efficient Approach (Large Files)
If your text file is large, using File.read might load the entire file into memory at once. Instead, read it line by line:
matches = [] File.foreach(filepath) do |line| matches += line.scan(/\b(?:aa|bb)\w*\b/) end puts matches.inspect
Customization Tip
If your words include special characters (like hyphens or periods), adjust the regex to include those. For example, if words can have hyphens:
matches = File.read(filepath).scan(/\b(?:aa|bb)[a-zA-Z0-9-]*\b/)
内容的提问来源于stack exchange,提问作者Varinder Sohal
相关产品推荐
相关产品推荐

