【问题标题】:unable to do multiple matches with regex pattern. pattern treats as a single match [duplicate]无法使用正则表达式模式进行多次匹配。模式视为单个匹配[重复]
【发布时间】:2019-09-09 00:54:39
【问题描述】:

我正在尝试匹配在文本中重复的文本模式。当我尝试匹配模式时,它匹配两者之间的任何内容。虽然我达到了预期的结果,但如果可能的话,我可能希望对我的正则表达式模式进行任何优化。请提出建议。

这是正文内容……

$text = @"

Microsoft (R) Windows Debugger Version 6.3.9600.17336 AMD64
Copyright (c) Microsoft Corporation. All rights reserved.


Loading Dump File [C:\Windows\MEMORY.DMP]
Kernel Summary Dump File: Only kernel address space is available


************* Symbol Path validation summary **************
Response                         Time (ms)     Location
Deferred                                       srv*DownstreamStore*https://msdl.microsoft.com/download/symbols

************* Symbol Path validation summary **************
Response                         Time (ms)     Location
Deferred                                       srv*DownstreamStore*https://msdl.microsoft.com/download/symbols
Symbol search path is: srv*DownstreamStore*https://msdl.microsoft.com/download/symbols
Executable search path is: 
Windows 7 Kernel Version 7600 UP Free x64
Product: LanManNt, suite: SmallBusiness TerminalServer SmallBusinessRestricted SingleUserTS
Built by: 7600.16385.amd64fre.win7_rtm.090713-1255
Machine Name:
Kernel base = 0xfffff800`01658000 PsLoadedModuleList = 0xfffff800`01895e50
Debug session time: Tue Apr 16 04:27:05.412 2019 (UTC - 7:00)
System Uptime: 7 days 1:02:26.286
Loading Kernel Symbols
...............................................................
..............................................Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
..................
.........
Loading User Symbols
PEB is paged out (Peb.Ldr = 000007ff`fffdb018).  Type ".hh dbgerr001" for details
Loading unloaded module list
....
*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

Use !analyze -v to get detailed debugging information.

BugCheck D1, {fffff8a0032dd010, 2, 0, fffff8800567d530}

*** ERROR: Module load completed but symbols could not be loaded for myfault.sys
Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
Probably caused by : myfault.sys ( myfault+1530 )

Followup: MachineOwner
---------

----- 64 bit Kernel Summary Dump Analysis

DUMP_HEADER64:
MajorVersion        0000000f
MinorVersion        00001db0
KdSecondaryVersion  00000000
DirectoryTableBase  00000000`cadb0000
PfnDataBase         fffffa80`00000000
PsLoadedModuleList  fffff800`01895e50
PsActiveProcessHead fffff800`01877b30
MachineImageType    00008664
NumberProcessors    00000001
BugCheckCode        000000d1
BugCheckParameter1  fffff8a0`032dd010
BugCheckParameter2  00000000`00000002
BugCheckParameter3  00000000`00000000
BugCheckParameter4  fffff880`0567d530
KdDebuggerDataBlock fffff800`01841070
SecondaryDataState  00000000
ProductType         00000002
SuiteMask           00000131

SUMMARY_DUMP64:
DumpOptions         504d4453
HeaderSize          00024000
BitmapSize          00108000
Pages               00013cb0
Bitmap.SizeOfBitMap 00108000

KiProcessorBlock at fffff800`01900900
  1 KiProcessorBlock entries:
  fffff800`01842e80


Windows 7 Kernel Version 7600 UP Free x64
Product: LanManNt, suite: SmallBusiness TerminalServer SmallBusinessRestricted SingleUserTS
Built by: 7600.16385.amd64fre.win7_rtm.090713-1255
Machine Name:
Kernel base = 0xfffff800`01658000 PsLoadedModuleList = 0xfffff800`01895e50
Debug session time: Tue Apr 16 04:27:05.412 2019 (UTC - 7:00)
System Uptime: 7 days 1:02:26.286
start             end                 module name
fffff800`0142c000 fffff800`01436000   kdcom     Mon Jul 13 18:31:07 2009 (4A5BDFDB)
fffff800`0160f000 fffff800`01658000   hal       Mon Jul 13 18:27:36 2009 (4A5BDF08)
fffff800`01658000 fffff800`01c35000   nt        Mon Jul 13 16:40:48 2009 (4A5BC600)
fffff880`00c00000 fffff880`00c3c000   vmbus     Mon Jul 13 16:42:54 2009 (4A5BC67E)
###

some similar text just to save characters

###
fffff960`00050000 fffff960`0035f000   win32k    Mon Jul 13 16:40:16 2009 (4A5BC5E0)
Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
fffff960`004c0000 fffff960`004de000   dxg       Mon Jul 13 16:38:28 2009 (4A5BC574)
fffff960`00620000 fffff960`0062a000   TSDDD     Mon Jul 13 17:16:34 2009 (4A5BCE62)
fffff960`008c0000 fffff960`008cb000   VMBusVideoD  Mon Jul 13 16:43:00 2009 (4A5BC684)
fffff960`00af0000 fffff960`00b26000   RDPDD     Mon Jul 13 17:16:54 2009 (4A5BCE76)

Unloaded modules:
fffff880`018e5000 fffff880`018f3000   crashdmp.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0000E000
fffff880`018f3000 fffff880`018ff000   dump_ataport.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0000C000
fffff880`018ff000 fffff880`01908000   dump_atapi.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  00009000
fffff880`00de5000 fffff880`00e00000   sacdrv.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0001B000
*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

Use !analyze -v to get detailed debugging information.

BugCheck D1, {fffff8a0032dd010, 2, 0, fffff8800567d530}

Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
Probably caused by : myfault.sys ( myfault+1530 )

Followup: MachineOwner
---------

Finished dump check
"@

这应该返回 2 个可能的匹配项

([regex]"(?ms)\*{20,}.+-{8,}").Matches($text)

但是给了这个..

*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

Use !analyze -v to get detailed debugging information.

BugCheck D1, {fffff8a0032dd010, 2, 0, fffff8800567d530}

*** ERROR: Module load completed but symbols could not be loaded for myfault.sys
Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
Probably caused by : myfault.sys ( myfault+1530 )

Followup: MachineOwner
---------

----- 64 bit Kernel Summary Dump Analysis

DUMP_HEADER64:
MajorVersion        0000000f
MinorVersion        00001db0
KdSecondaryVersion  00000000
DirectoryTableBase  00000000`cadb0000
PfnDataBase         fffffa80`00000000
PsLoadedModuleList  fffff800`01895e50
PsActiveProcessHead fffff800`01877b30
MachineImageType    00008664
NumberProcessors    00000001
BugCheckCode        000000d1
BugCheckParameter1  fffff8a0`032dd010
BugCheckParameter2  00000000`00000002
BugCheckParameter3  00000000`00000000
BugCheckParameter4  fffff880`0567d530
KdDebuggerDataBlock fffff800`01841070
SecondaryDataState  00000000
ProductType         00000002
SuiteMask           00000131

SUMMARY_DUMP64:
DumpOptions         504d4453
HeaderSize          00024000
BitmapSize          00108000
Pages               00013cb0
Bitmap.SizeOfBitMap 00108000

KiProcessorBlock at fffff800`01900900
  1 KiProcessorBlock entries:
  fffff800`01842e80


Windows 7 Kernel Version 7600 UP Free x64
Product: LanManNt, suite: SmallBusiness TerminalServer SmallBusinessRestricted SingleUserTS
Built by: 7600.16385.amd64fre.win7_rtm.090713-1255
Machine Name:
Kernel base = 0xfffff800`01658000 PsLoadedModuleList = 0xfffff800`01895e50
Debug session time: Tue Apr 16 04:27:05.412 2019 (UTC - 7:00)
System Uptime: 7 days 1:02:26.286
start             end                 module name
fffff800`0142c000 fffff800`01436000   kdcom     Mon Jul 13 18:31:07 2009 (4A5BDFDB)
fffff800`0160f000 fffff800`01658000   hal       Mon Jul 13 18:27:36 2009 (4A5BDF08)
fffff800`01658000 fffff800`01c35000   nt        Mon Jul 13 16:40:48 2009 (4A5BC600)
fffff880`00c00000 fffff880`00c3c000   vmbus     Mon Jul 13 16:42:54 2009 (4A5BC67E)
fffff880`00c3c000 fffff880`00c66000   ataport   Mon Jul 13 16:19:52 2009 (4A5BC118)
####

Some similar text just to save characters

####
Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
fffff960`004c0000 fffff960`004de000   dxg       Mon Jul 13 16:38:28 2009 (4A5BC574)
fffff960`00620000 fffff960`0062a000   TSDDD     Mon Jul 13 17:16:34 2009 (4A5BCE62)
fffff960`008c0000 fffff960`008cb000   VMBusVideoD  Mon Jul 13 16:43:00 2009 (4A5BC684)
fffff960`00af0000 fffff960`00b26000   RDPDD     Mon Jul 13 17:16:54 2009 (4A5BCE76)

Unloaded modules:
fffff880`018e5000 fffff880`018f3000   crashdmp.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0000E000
fffff880`018f3000 fffff880`018ff000   dump_ataport.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0000C000
fffff880`018ff000 fffff880`01908000   dump_atapi.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  00009000
fffff880`00de5000 fffff880`00e00000   sacdrv.sys
    Timestamp: unavailable (00000000)
    Checksum:  00000000
    ImageSize:  0001B000
*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

Use !analyze -v to get detailed debugging information.

BugCheck D1, {fffff8a0032dd010, 2, 0, fffff8800567d530}

Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
Probably caused by : myfault.sys ( myfault+1530 )

Followup: MachineOwner
---------

但我最终这样做是为了获得成功的第二部分

$text -match [regex]"(?ms)\*{20,}.+-{8,}\s+\n+-.+\n(^\*{20,}.+\*\s+[bB].+\*{20,}.+-{8,})" 
$Matches[1]

想要的结果只是第二部分

*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

Use !analyze -v to get detailed debugging information.

BugCheck D1, {fffff8a0032dd010, 2, 0, fffff8800567d530}

Page eb21d not present in the dump file. Type ".hh dbgerr004" for details
Probably caused by : myfault.sys ( myfault+1530 )

Followup: MachineOwner
---------

【问题讨论】:

    标签: c# regex powershell


    【解决方案1】:

    您可以将第一个正则表达式稍微修改为使用惰性量词.*?。然后选择两个匹配项的第二个索引值[1]

    ([regex]"(?ms)\*{20,}.*?-{8,}").matches($text).value[1]
    

    如果你想要两个匹配,那么就不要索引了:

    ([regex]"(?ms)\*{20,}.*?-{8,}").matches($text).value
    

    【讨论】:

    • 非常感谢。并且有效。什么地方出了错?你能解释一下吗?
    • 他已经做到了(+1)。 .* 默认情况下是贪婪的 - 它需要它可以获得的最大匹配。在这里,它跨越了您预期的两个匹配项,问号 .*? 改变了这种行为。
    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多