Jump to content
Larry Ullman's Book Forums

Another Regex Question (I Really Need Your Help This Time)


Recommended Posts

Perhaps what I'm trying to do is not possible with regexes, but I'm gonna ask anyway, as more likely, I'm just incapable of thinking of a decent regex.

 

Let's say we have the following code within an HTML file:

 

<ul id="1">

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

</ul>

<ul id="2">

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

</ul>

<ul id="3">

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

 <li>List item</li>

</ul>

 

I want to come up with a regex that'll capture all six <li> tags within the id="2" ul. Is this possible?

Link to comment
Share on other sites

That's a good point, except I don't know how to change a string containing HTML data into XML data. Do you?

 

Also, the thought had crossed my mind of doing one regex search to grab the substring <ul id="2">...</ul>, and then parsing within that, but I suppose I'd rather just use one regex, if possible...if only for a challenge.

Link to comment
Share on other sites

Also, I should mention that the reason I'm doing this is that I want to be able to dynamically count the number of li tags within certain uls. The other trick though is that I don't know how many lis there actually are, and it changes all the time.

 

I suppose it would have been helpful if I had better explained the situation at the start. Sorry.

Link to comment
Share on other sites

To answer your question - yes it is possible with a regular expression and its relatively straight forward - also depending on what you want to do with the values after you might be able to just do it client side with jQuery because it would be extremely simple.

 

$('li [id="2"] > li').length;

 

If you wanted to use regex the starting point would be extracting the list with an ID of 2 and then creating sub-expressions to grab the li elements. Then use preg_match_all to extract them and apply PHP's count to the relevant branch of the returned array. If you're having no luck getting it working post your regex and we can try and help you out.

  • Upvote 1
Link to comment
Share on other sites

Yeah, I know jQuery makes it simple; almost too simple for my tastes. What I mean by that is that I want to challenge myself.

 

Anyway, if I use two expressions, one to grab the id="2" ul, and one to parse the content, it's pretty simple. First I do a substring match with the following regex:

 

<ul id="2">[\s\S]*?<\/ul>

 

And then I simply do I global search for the li tags, as such:

 

<li>

 

Well, the pattern is /<li>/g, but the PHP functions work differently.

 

Anyway, my point is, that's a piece of cake, but I'm thinking that where two expressions suffice, there must be a way to do it with just one.

Link to comment
Share on other sites

 Share

×
×
  • Create New...