views:

58

answers:

3

I'm trying to create a Regular Expression to match "wiki style" lists as in (using preg_replace_callback() ):

* List Item 1  
* List Item 2  
*# List Item 2.1  
*# List Item 2.2  
* List Item 3 

Asterisks denote Unordered Lists while Number-Signs denote Ordered Lists. I'm trying to get this so it can match infinite depth and so that * and # can be mixed.

I tried the following expression (and variations of it): /\s([\*#]{1,}) ([\S ]+)\s/si
But it doesn't seem to want to work.

What am I doing wrong? Or is there a better way of accomplishing this?

A: 

Try something like this:

\s*([*#]+)\s+.*
polygenelubricants
I did try that, and what happened was that it matched up to a certain point in my list and then it matched the rest of it in another callback-replace. Essentially what it did was split my list.
Syd Miller
A: 

Not sure exactly if I understand what you are looking for, but doesn't ([*#]{1,}) ([\S ]+) work? I think this will give you a match on * and *# for group 1 and "List Item 2.1" for group 2, if that is what you are looking for. I know this is essentially what you had, but I am not having any problem with it when testing it out.

It does work for me as well. I thought the "\s" prepended and appended to the pattern were necessary so that this pattern doesn't match instances of "G**gle" (for example). With that said, it is working for me as well.
Syd Miller
A: 

Regular expressions are not generally recursive. You need to set up a more traditional parsing approach (perhaps nested state machine using regular expressions to control transitions).

John
This is actually what I'm doing. I'm using a regular expression to match "* foo" or "# foo" or *#**#* foo" and then passing the results through a callback function which will build the list-markup.I was just wondering if there was a more "elegant" way of doing this.
Syd Miller