Since the line number is important to you, not the actual contents of the div, I would be inclined not to use the regex at all. I would suggest that explode() string into an array and loop through this array looking for your marker. For instance:
<?php $myContent = "[your string of html here]"; $myArray = explode("\n", $myContent); $arraylen = count($myArray);
Disclaimer: I do not have a php installation available for testing, so some debugging may be required.
Hope this helps, as I think itβs just a waste of time for you to implement a parsing mechanism just to make something so simple, especially if itβs one-time.
Edit: If the content at this stage is impotent, you can use it in combination with other answers that provide an adequate regular expression for the job.
Edit # 2: Oh hey ... here are my two cents:
"/<div.*?id=\"Alpha\".*?>.*?(<div.*//div>)*.*?//div>/m"
(<div.*//div>) tells the regex engine that it can find nested div tags and just include them if it finds them, rather than just stopping at the first </div> . However, this solves the problem only if there is only one breeding level. If there is more, then the regular expression is not a pity for you: (.
/m also causes the regex engine to ignore line strings, so you don't need to pollute your expressions with [\S\s] everywhere.
Again, sorry, I do not have an environment to check this at the moment, so you may need debugging.
Greetings Ian
source share