我有以下示例字符串ABC__hdsiugid_23123_FGH1_sdfkjk_FGH2
。
我想要做的是同时捕获FGH1
和FGH2
,同时确保我的模式以ABC
开头。
当我尝试延迟模式ABC.+?(FGH\d)
时,我得到FGH1
并且使用贪婪模式ABC.+(FGH\d)
,我得到FGH2
。如何修改模式以捕获FGH1
和FGH2
?
Sub RexTest()
Dim rex As New RegExp
rex.Pattern = "ABC.+?(FGH\d)" ' or "ABC.+(FGH\d)"
rex.Global = True
Dim str As String: str = "ABC__hdsiugid_23123_FGH1_sdfkjk_FGH2"
Dim mtch As Object
For Each mtch In rex.Execute(str)
Debug.Print mtch.SubMatches(0)
Next
End Sub
编辑:我意识到我应该让我的问题更清楚(谢谢sln)。在我给出的示例字符串中,只有2个FGH [0-9],但实际上可能存在任意数量。
答案 0 :(得分:1)
你可以使用这样的正则表达式:
^(?:(?!ABC).)*|(FGH\d)
<强> Working demo 强>
MATCH 1
1. [20-24] `FGH1`
MATCH 2
1. [32-36] `FGH2`
MATCH 3
1. [51-55] `FGH3`
MATCH 4
1. [80-84] `FGH4`
MATCH 5
1. [92-96] `FGH5`
MATCH 6
1. [117-121] `FGH6`
答案 1 :(得分:0)
你提到了VSTO。如果你能做到这一点,你可以从vba运行C#段 你如何整理结果超出了我。
反正。这是一个真正简单的正则表达式样本,它利用捕获集合一个特征 应该在所有引擎中,但我猜只有Dot-Net。
通常,每次运行群集组表达式时都会覆盖捕获缓冲区,但是 MS只是将结果累积在一个数组中。
这是......
C#代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Regex FghRx = new Regex(
@"
^ # Beginning of Line
ABC # Must be an 'ABC' at bol
(?: # START Cluster group
.*? # optional non-'FGH' (and not newlines)
( FGH \d+ ) # (1), The FGH Capture Collection
)+ # END Cluster group, do many times
"
, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
string FghData =
"ABC__hdsiugid_23123_FGH10_sdfkjk_FGH20 \n" +
"ABC__hdsiugid_23123_FGH11_sdfkjk_FGH21_dopqw_FGH31 \n" +
"ABC__hdsiugid_23123_FGH12_sdfkjk_FGH22_dopqw_FGH32 \n" +
"333333__ABC__hdsiugid_23123_FGH120_sdfkjk_FGH220_dopqw_FGH320 \n" +
"ABC__hdsiugid_23123_FGH13_sdfkjk_FGH23_dopqw_FGH33_dopqw_FGH43 \n" +
"ABC__hdsiugid_23123_FGH14_sdfkjk_FGH24_dopqw_FGH34_dopqw_FGH44 \n" +
"333333__ABC__hdsiugid_23123_FGH121_sdfkjk_FGH221_dopqw_FGH321 \n" +
"ABC__hdsiugid_23123_FGH15_sdfkjk_FGH25_dopqw_FGH35 \n" +
"ABC__hdsiugid_23123_FGH16_sdfkjk_FGH26_dopqw_FGH36 \n" ;
Match FghMatch = FghRx.Match( FghData );
while ( FghMatch.Success )
{
Console.WriteLine( "New Record\n------------------------" );
CaptureCollection cc_fgh = FghMatch.Groups[1].Captures;
for (int i = 0; i < cc_fgh.Count; i++)
{
Console.WriteLine( "'{0}'", cc_fgh[i].Value );
}
FghMatch = FghMatch.NextMatch();
Console.WriteLine( "------------------------\n" );
}
return;
}
}
}
输出&gt;&gt;
New Record
------------------------
'FGH10'
'FGH20'
------------------------
New Record
------------------------
'FGH11'
'FGH21'
'FGH31'
------------------------
New Record
------------------------
'FGH12'
'FGH22'
'FGH32'
------------------------
New Record
------------------------
'FGH13'
'FGH23'
'FGH33'
'FGH43'
------------------------
New Record
------------------------
'FGH14'
'FGH24'
'FGH34'
'FGH44'
------------------------
New Record
------------------------
'FGH15'
'FGH25'
'FGH35'
------------------------
New Record
------------------------
'FGH16'
'FGH26'
'FGH36'
------------------------
Press any key to continue . . .