Regex scenarios to Match HTML Tags with Attributes in Ruby - Need guide with Complex Patterns
I've been researching this but Does anyone know how to I'm trying to extract specific HTML tags that contain certain attributes using regex in Ruby, but I'm running into unexpected behavior. My goal is to find all `<div>` elements that have a `class` attribute with the value `container` and then capture the inner content. Here's the regex pattern I've been using: ```ruby regex = /<div\s+[^>]*class=['"]container['"][^>]*>(.*?)<\/div>/i ``` However, when I apply this pattern, I'm not getting the expected matches from the following HTML snippet: ```html <div class="container"> <p>Sample text</p> </div> <div class="container" id="main"> <p>Another sample text</p> </div> <div class="wrapper"> <p>This should not match</p> </div> ``` When I run the regex against this HTML, it only captures the first `<div>` tag correctly, but fails to capture the content of the second `<div>` with the `id` attribute. The weird part is that if I remove the `id` attribute from the second `<div>`, it works perfectly. However, I need to include both tags regardless of additional attributes. I've also tried modifying the regex to be more permissive with the attributes: ```ruby regex = /<div\s+class=['"]container['"][^>]*>(.*?)<\/div>/mi ``` But this still doesn't resolve the scenario. I'm aware that regex isn't the best tool for parsing HTML, but I want to use a full HTML parser for this specific task, so I'm looking for help to refine my regex. Is there a way to adjust it so that it matches `<div>` tags correctly, even when they have extra attributes? Any tips on improving performance or avoiding backtracking issues would also be appreciated. Any pointers in the right direction? I appreciate any insights!