给出以下字符串:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla rhoncus ipsum a 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 a eros tincidunt ultricies...
我正在尝试生成一个返回的正则表达式:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla rhoncus ipsum a 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 a eros tincidunt ultricies...
每个字符串中可能有一个或多个菜单块,每个块中有一个未知数量的链接。我可以交换整个块,但是在替换块中的各个链接时会掉头。
有没有办法在一个或多个正则表达式中执行此操作(我希望尽可能避免将字符串拆分为数组)。我正在使用vbScript,但是一个通用的例子也同样有用。
答案 0 :(得分:1)
一种(有点复杂)的方式:
Option Explicit
Dim g_reItem ' sorry, must be 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 "------------------"
输出:
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
------------------
答案 1 :(得分:0)
[Pseudo]
假设一些事情:
-It is always going to be urls between the menus
-You want the end of each to be item [position]
-Each item starts with item[number]
我给你的建议就是一步一步:
menu]
并替换为ul]
,这应该很容易item\s?\d.*
item\s?\d
与<a href="
的匹配,然后将其连接到字符串">Item [array position(plus 1 if of zero index)]</a>
答案 2 :(得分:0)
您可以使用/(.+)\|(.+)/
作为正则表达式,然后<a href="$2">$1</a>
作为替换来获取项目...
然后只需使用普通字符串替换来更改[menu]
和[/menu]