regex - Replacing the contents of a list of items using regular expressions -
given following string:
lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla rhoncus ipsum eros tincidunt ultricies... [menu] item 1|http://stackoverflow.com/1 item 2|http://stackoverflow.com/2 item 3|http://stackoverflow.com/3 [/menu] lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla rhoncus ipsum eros tincidunt ultricies...
i'm trying generate regular expression returns:
lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla rhoncus ipsum eros tincidunt ultricies... <ul> <a href="http://stackoverflow.com/1">item 1</a> <a href="http://stackoverflow.com/2">item 2</a> <a href="http://stackoverflow.com/3">item 3</a> </ul> lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla rhoncus ipsum eros tincidunt ultricies...
there 1 or more menu blocks in each string , unknown number of links within each block. i'm able swap whole block, falling on over replacing individual links within block.
is there way of doing in 1 or more regular expressions (i prefer avoid splitting string array if @ possible). i'm using vbscript, generic example useful.
one (a bit complicated) way it:
option explicit dim g_reitem ' sorry, must global class cremagic private m_remenu private sub class_initialize() set m_remenu = new regexp m_remenu.global = true m_remenu.ignorecase = true m_remenu.multiline = true m_remenu.pattern = "^\[menu\]([\s\s]+?)^\[/menu\]$" set g_reitem = new regexp g_reitem.global = true g_reitem.ignorecase = true g_reitem.multiline = true ' item 1|http://stackoverflow.com/1 ' <a href="http://stackoverflow.com/1">item 1</a> g_reitem.pattern = "^\s*([^|]+)\|(http:.+?)(\r)$" end sub public function replace(sinp) replace = m_remenu.replace(sinp, getref("menurpl")) end function end class function menurpl(sm, sg1, npos, ssrc) menurpl = "<ul>" & g_reitem.replace(sg1, getref("itemrpl")) & "</ul>" end function function itemrpl(sm, sg1, sg2, sg3, npos, ssrc) itemrpl = vbcrlf & "<a href=""" & sg2 & """>" & sg1 & "</a>" end function dim sinp : sinp = join(array( _ "lorem ipsum dolor sit amet, consecte" _ , "" _ , "[menu]" _ , "item 1|http://stackoverflow.com/1" _ , "item 2|http://stackoverflow.com/2" _ , "item 3|http://stackoverflow.com/3" _ , "[/menu]" _ , "" _ , "[mune]" _ , "item 1|http://stackoverflow.com/1" _ , "item 2|http://stackoverflow.com/2" _ , "item 3|http://stackoverflow.com/3" _ , "[/mune]" _ , "[menu]" _ , "item 4|http://stackoverflow.com/4" _ , "[/menu]" _ , "" _ , "lorem ipsum dolor sit amet, consecte" _ ), vbcrlf) wscript.echo sinp wscript.echo "------------------" dim oremagic : set oremagic = new cremagic wscript.echo oremagic.replace(sinp) wscript.echo "------------------"
output:
lorem ipsum dolor sit amet, consecte [menu] item 1|http://stackoverflow.com/1 item 2|http://stackoverflow.com/2 item 3|http://stackoverflow.com/3 [/menu] [mune] item 1|http://stackoverflow.com/1 item 2|http://stackoverflow.com/2 item 3|http://stackoverflow.com/3 [/mune] [menu] item 4|http://stackoverflow.com/4 [/menu] lorem ipsum dolor sit amet, consecte ------------------ lorem ipsum dolor sit amet, consecte <ul> <a href="http://stackoverflow.com/1">item 1</a> <a href="http://stackoverflow.com/2">item 2</a> <a href="http://stackoverflow.com/3">item 3</a> </ul> [mune] item 1|http://stackoverflow.com/1 item 2|http://stackoverflow.com/2 item 3|http://stackoverflow.com/3 [/mune] <ul> <a href="http://stackoverflow.com/4">item 4</a> </ul> lorem ipsum dolor sit amet, consecte ------------------
Comments
Post a Comment