正则表达式选择两个“字符串”

时间:2014-01-21 23:27:51

标签: c# regex

我有一个文件列表,当有人启动程序时,它将获得“普通文件”,当有人进行全面检查时,它将获得“完整检查文件”。

[normalFiles]
element\userdata\systemsettings.ini
element\userdata\server\serverlist.txt
element\data\elements.data
element\data\gshop.data
element\data\gshop1.data
element\data\gshop2.data
element\elementskill.dll
element\configs.pck
element\monkeydynasty.exe
element\data\aipolicy.data
element\data\domain.data
element\data\domain1.data
element\data\domain2.data
element\data\domain3.data
element\data\domain4.data
element\data\dyn_tasks.data
element\data\dynamicobjects.data
element\data\forbidden_task.txt
element\data\hometowndata
element\data\path.data
element\data\task_npc.data
element\data\tasks.data
element\data\tasks.data1
element\data\tasks.data10
element\data\tasks.data11
element\data\tasks.data12
element\data\tasks.data13
element\data\tasks.data14
element\data\tasks.data15
element\data\tasks.data16
element\data\tasks.data17
element\data\tasks.data18
element\data\tasks.data19
element\data\tasks.data2
element\data\tasks.data20
element\data\tasks.data21
element\data\tasks.data22
element\data\tasks.data23
element\data\tasks.data24
element\data\tasks.data25
element\data\tasks.data26
element\data\tasks.data27
element\data\tasks.data28
element\data\tasks.data29
element\data\tasks.data3
element\data\tasks.data30
element\data\tasks.data31
element\data\tasks.data32
element\data\tasks.data33
element\data\tasks.data34
element\data\tasks.data35
element\data\tasks.data36
element\data\tasks.data37
element\data\tasks.data38
element\data\tasks.data39
element\data\tasks.data4
element\data\tasks.data40
element\data\tasks.data41
element\data\tasks.data42
element\data\tasks.data43
element\data\tasks.data5
element\data\tasks.data6
element\data\tasks.data7
element\data\tasks.data8
element\data\tasks.data9
element\data\title_def.lua
element\data\title_def_u.lua
element\trees.pck
element\data\vipaward.data
element\reportbugs\zreportbugs.exe
element\interfaces.pck
[/normalFiles]
[FullCheckFiles]
element\interfaces.pck
element\Localize.ini
element\arcsdk.dll
element\avcodec-52.dll
element\avformat-52.dll
element\avutil-50.dll
element\bbsfiles\error.html
element\bbsfiles\extensions\extbank\bank3.dll
element\bbsfiles\extensions\extbank\bankbox.ini
element\bbsfiles\extensions\extbank\banklist.dll
element\bbsfiles\extensions\extbank\extbank.ini
element\bbsfiles\extensions\extbank\stat.ini
element\bbsfiles\extensions\extbank\stat_bankbox.ini
element\bbsfiles\extensions\extbank\up_temp.ini
element\berkelium.dll
element\berkelium.exe
element\building.pck
element\chrome.log
element\chrome_plugins_file.xml
element\coreclientsdk.dll
element\cursors\attack.ani
element\cursors\attack.cur
element\cursors\axe.ani
element\cursors\choose.ani
element\cursors\choose1.ani
element\cursors\choose2.ani
element\cursors\dig.ani
element\cursors\drag.ani
element\cursors\drag.cur
element\cursors\drag1.ani
element\cursors\fire.ani
element\cursors\flag.cur
element\cursors\gear.ani
element\cursors\geer.ani
element\cursors\hammer.ani
element\cursors\hand.cur
element\cursors\hand1.ani
element\cursors\hand2.ani
element\cursors\harvest1.ani
element\cursors\harvest2.ani
element\cursors\hook.ani
element\cursors\key.ani
element\cursors\knife.ani
element\cursors\normal.ani
element\cursors\pest1.ani
element\cursors\pest2.ani
element\cursors\pick.ani
element\cursors\pick.cur
element\cursors\plot.ani
element\cursors\plot1.ani
element\cursors\plot2.ani
element\cursors\point.ani
element\cursors\repair.cur
element\cursors\seed.ani
element\cursors\seed1.ani
element\cursors\seed2.ani
element\cursors\split.ani
element\cursors\steal1.ani
element\cursors\steal2.ani
element\cursors\talk.cur
element\cursors\unablepoint.ani
element\cursors\water1.ani
element\cursors\water2.ani
element\cursors\weed1.ani
element\cursors\weed2.ani
element\cursors\zoomin.cur
element\cursors\zoomout.cur
element\d3dx9_24.dll
element\dbghelp.dll
element\dbserver.conf
element\facedata.pck
element\font\fzl2jw.ttf
element\font\fzl2jw.ttf1
element\font\fzlbjw.ttf
element\font\fzlbjw.ttf1
element\font\fzxh1jw.ttf
element\ftdriver.dll
element\gfx.pck
element\grasses.pck
[/FullCheckFiles]

我尝试过以下表达式

(?=[normalFiles])(.*?element(?:(?!\|).)*)(?![/normalFiles])

仅在[normalFiles][/normalFiles]之间获取文件名,但它也会在[FullCheckFiles][/FullCheckFiles]之间获取文件名。

2 个答案:

答案 0 :(得分:0)

您对lookaround使用了错误的符号。积极的向后看是(?<=x),而积极的向前看是(?=x)。此外,[]用于表示正则表达式中的字符类,因此如果它们以文字形式出现,则需要将它们转义。

(?<=\[normalFiles\])(.*?)(?=\[/normalFiles\])

编辑:我正在使用以下代码进行测试:

var match = Regex.Match(filesList, @"(?<=\[normalFiles\])(.*?)(?=\[/normalFiles\])", 
                        RegexOptions.Singleline);
if (match.Success)
    Console.WriteLine(match.Value);

答案 1 :(得分:0)

您可以使用:

(?:\[normalFiles\])((\n|.)*)(?=\[\/normalFiles\])

Demo

说明:

enter image description here