Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用Regex.Replace时如何处理多个匹配_.net_Regex_Vb.net - Fatal编程技术网

.net 使用Regex.Replace时如何处理多个匹配

.net 使用Regex.Replace时如何处理多个匹配,.net,regex,vb.net,.net,Regex,Vb.net,我有一个正则表达式,它会导致多个匹配。示例数据集是一个CSV文件,每一行都是一个单独的匹配项: product,color,type,shape,size apple,green,fruit,round,large banana,yellow,fruit,long,large cherry,red,fruit,round,small 因此,匹配1是苹果、绿色、水果、圆形、大型,匹配2是香蕉、黄色、水果、长形、大型,等等 因此,我的问题是,在使用RegEx.Replace时,如何指定“开始”匹配

我有一个正则表达式,它会导致多个匹配。示例数据集是一个CSV文件,每一行都是一个单独的匹配项:

product,color,type,shape,size
apple,green,fruit,round,large
banana,yellow,fruit,long,large
cherry,red,fruit,round,small
因此,匹配1是苹果、绿色、水果、圆形、大型,匹配2是香蕉、黄色、水果、长形、大型,等等

因此,我的问题是,在使用RegEx.Replace时,如何指定“开始”匹配(例如,在本例中,我希望从第二个匹配开始),以及如何指定之后的#个匹配?这只是一个例子,在其他场景中,我想从match#4等开始

它似乎支持类似的东西,但我正在寻找一个更好的例子,适用于我的场景

我试过:

Dim r As New RegEx(pattern)
result = r.Replace(input, replace, 1, 2)
replace是一个字符串,它包含一个捕获的值(在我的例子中是1美元),但我没有看到任何不同,仍然在一个字符串中获取所有匹配项


有什么建议吗?我希望可以做一些简单的事情,比如获取匹配项的#,然后使用For循环。

看看
Regex.Replace(string,string,MatchEvaluator)


这应该允许您传递一个MatchEvaluator来检查特定匹配的索引,因此在这种情况下,您可以查找
索引==1

我不会仅使用正则表达式来标识文本中的行。使用以下命令读取CSV文件:

Dim lines As String()

lines = File.ReadAllLines("path of the CSV file")
然后像这样循环

For i As Integer = starting_match To last_match
    lines(i) = lines(i).Replace("old","new")
Next
把这些线和

Dim result As String
result = String.Join(System.Environment.NewLine, lines)

更新

混淆的原因是
Replace
方法中的起始位置表示起始字符位置,而不是起始匹配索引。因此,我建议使用这种扩展方法

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ReplaceMatches(regex As Regex,
                                      input As String, replacement As String, 
                                      countMatches As Integer, startAtMatch As Integer
                                     ) As String
    Dim matches As MatchCollection = regex.Matches(input)
    If startAtMatch >= matches.Count Then
        Return input
    End If
    Dim skippedMatch As Match = matches(startAtMatch - 1)
    Dim startAtCharacterPosition As Integer = skippedMatch.Index + skippedMatch.Length
    Return regex.Replace(input, replacement, countMatches, startAtCharacterPosition)
End Function

(使用从C#转换为VB的示例)

以下代码可能会对您有所帮助


试试看。第二个参数不是图案,而是替换。不清楚你的图案是什么,输入是什么?@Olivier:你说得对,我把那个弄糟了,谢谢!所以我刚刚更新了我的代码,但看起来我指定的数值没有影响。按照我指定的方式,它不会捕获1个匹配,从第2个匹配开始吗?
count
参数表示字符位置,而不是匹配索引。请看我答案的更新。这只是一个例子,它并不总是一个CSV文件(事实上,在大多数情况下,它甚至不是一个文件),它可能是一堆HTML,我试图在其中捕获
  • 组。好的,所以我尝试了这个例子,它只会导致另一个错误。函数f(m作为匹配)控制台.WriteLine(“执行f()”)结束函数Dim r作为新正则表达式(sRegExPattern)Dim e作为匹配计算器=ProcessRegExMatches结果的地址=r.Replace(“字母-字母-缺少-缺少-释放)”,“\w+”,e)我得到:脚本编译错误:方法“公共函数ProcessRegExMatches”(m As System.Text.RegularExpressions.Match)As Object“没有与委托相同的签名”委托函数MatchEvaluator(与System.Text.RegularExpressions.Match匹配)As String“我在你上面的帖子中包含了成本,但网站不允许我在回复中格式化代码。
    Dim input As String = "aaa bbb ccc ddd eee fff"
    Dim startAtMatch As Integer = 2 ' ccc
    Dim countMatches As Integer = 3
    
    Dim regex = New Regex("\w+")
    Dim result As String = regex.ReplaceMatches(input, "XX", countMatches, startAtMatch)
    Console.WriteLine(result) ' --> "aaa bbb XX XX XX fff"
    
    Imports System.Collections
    
    Imports System.Text.RegularExpressions
    
    Module Example
    
        Public Sub Main()
            Dim words As String = "letter alphabetical missing lack release " + _
                                  "penchant slack acryllic laundry cease"
            Dim pattern As String = "\w+  # Matches all the characters in a word."
            Dim evaluator As MatchEvaluator = AddressOf WordScrambler
            Console.WriteLine("Original words:")
            Console.WriteLine(words)
            Console.WriteLine("Scrambled words:")
            Console.WriteLine(Regex.Replace(words, pattern, evaluator,
                                            RegexOptions.IgnorePatternWhitespace))
        End Sub
    
        Public Function WordScrambler(ByVal match As Match) As String
            Dim arraySize As Integer = match.Value.Length - 1
            ' Define two arrays equal to the number of letters in the match. 
            Dim keys(arraySize) As Double
            Dim letters(arraySize) As Char
    
            ' Instantiate random number generator' 
            Dim rnd As New Random()
    
            For ctr As Integer = 0 To match.Value.Length - 1
                ' Populate the array of keys with random numbers.
                keys(ctr) = rnd.NextDouble()
                ' Assign letter to array of letters.
                letters(ctr) = match.Value.Chars(ctr)
            Next
            Array.Sort(keys, letters, 0, arraySize, Comparer.Default)
            Return New String(letters)
        End Function
    
    End Module
    
    ' The example displays output similar to the following: 
    '    Original words: 
    '    letter alphabetical missing lack release penchant slack acryllic laundry cease 
    '     
    '    Scrambled words: 
    '    etlert liahepalbcat imsgsni alkc ereelsa epcnnaht lscak cayirllc alnyurd ecsae