Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular expression problem (PERL Syntax)

Parse list and convert to HTML format



11:15 am on Feb 22, 2005 (gmt 0)

10+ Year Member


I'm having a tough problem that I want to solve with regular expressions. I'm pretty sure that it is possible for a regex to parse this thing, but since I only know the basic stuff, Im pretty much lost here.

I want to parse a string like this:

#list item1
#list item2
#list item3

And this is what I want the regex to return:

<li>list item1</li>
<li>list item2</li>
<li>list item3</li>

Basically, I want to parse my listing and return a valid HTML unordered list. The regex has to find start end end tags (##) and then parse the inner part to find strings like #text\n where \n is a line break.

Can anyone help me with this?

Thanks in advance!


2:29 am on Feb 23, 2005 (gmt 0)

10+ Year Member

This function will work for one list per text call:

function convert_to_list($text)
$pattern1 = "/(\#\#\n)((\#{1})[\w?\s?\W?\S?]+\n)*(\#\#\n)/";
$replace1 = "<ul>$2</ul>";

$list = preg_replace($pattern1, $replace1, $text);

$pattern2 = "/(\#{1})(\w{1,}.+)?\n/";
$replace2 = "<li>\\2</li>";

$text = preg_replace($pattern2, $replace2, $list);
return $text;

I haven't been able to get it working for multiple lists in the same piece of text... perhaps someone here is able to tweak the regex a little so it will do multiple replacements successfully?

I'm still discovering regex's myself, so it's probably not that efficient. :(


7:42 am on Feb 23, 2005 (gmt 0)

10+ Year Member

Thank you!

I don't understand the regex completely, but I know what it does ;)

It would be great if anyone could help getting this regex work with multiple lists in a text string.



9:18 pm on Feb 23, 2005 (gmt 0)

10+ Year Member

If you want to learn some more about regex's heres a couple of links. Handy to know, especially when filtering and validating content:

Sample regex's (regex library)

In depth Regex tutorial

O'reilly regex pocket reference (PDF)

More regex references (this is probably the best one for beginners)


11:10 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Welcome to WebmasterWorld ironik


11:41 pm on Feb 23, 2005 (gmt 0)

10+ Year Member

Thanks ;)

Been lurking for a while, thought it was time to sign up!


11:46 pm on Feb 23, 2005 (gmt 0)

10+ Year Member

You made me curious if that's possible as a single regex. This seems to work:

#!/usr/bin/perl -w
#list item1
#list item2
#list item3
another list
#another 1
#another 2

{ local $prefix=$1 . '<UL>' . $2;
local $suffix='</UL>' . $4;
(local $lines=$3)=~s/^#/<LI>/mg;
$prefix . $lines . $suffix;

print $a;

As a bonus you get 'gsex' (great sex) as a regex qualifier :)


12:51 pm on Feb 24, 2005 (gmt 0)

10+ Year Member

@andrew_m: Thanks for your help! I honestly have no idea what your regex is doing for most of the time.

I tried to run it as a PERL script, but it didnt work. The error was: Can't find string terminator "EOT" anywhere before EOF at ./perltest line 2.

How can I get this working inside PHP's preg_replace() function?



9:50 pm on Feb 24, 2005 (gmt 0)

10+ Year Member

I have no idea, sorry, don't know a thing about php. I thought you asked about perl and it does work fine in perl -- it's cut-n-paste from the actual script that I ran to test it.