How to RegEx Replace Named Groups

I need to use RegEx.Replace to replace only specific named groups in my input line.

So, I can have a template like this:

"^(?<NoReplace>.+)(?<FirstPeriod>(\d{2})|CM|RM|PM|CN|RN){1}(?<LastPeriod>(\d{2})|CM|RM|PM|CN|RN){1}((#(?<NumberFormat>[#,\.\+\-%0]+))*)$"

Tokens like CM, RM are replaced using Regex.Replace with MatchEvaluator. However, this should only be replaced with characters in the FirstPeriod and LastPeriod groups.

Input Example: "FIELDCNS 01CM"

Required Conclusion: "FIELDCNS 0104"

Invalid output: "FIELD**04**S 0104"

Is this possible, or is it best for me to just pull out the parts that I want to replace and reassemble after?

+3
source share
5 answers

, , , , , , , . , "blah" "XXXXX", "foo" ""bar", :

Dim regex As Regex = new Regex("(foo.*)blah(.*bar)")
Console.WriteLine(regex.Replace( _
    "blah foo bar baz blah baz bar blah blah foo blah", "$1XXXXX$2"))
Console.ReadLine()

blah foo bar baz XXXXX baz bar blah blah foo blah

+5

, . , , , . , .

(?<=.)(\d{2})(?=(\d{2}|CM|RM|PM|CN|RN)|(((#(?<NumberFormat>[#,\.\+\-%0]+))*)$))

", , ( CM RM...) ( )" . lookahead (?=) lookbehind (?<=) , .

, :

"FIELDCNS 01CM02CN"

MatchEvaluator, :

"FIELDCNS XXCMYYCN"

"01" "04", MatchEvaluator.

+1

Replace String.Remove, , , .

Public Function ReplaceGroup(ByVal regexp As Text.RegularExpressions.Regex, ByVal input As String, ByVal group As String, ByVal replacement As String) As String
    Dim match As Text.RegularExpressions.Match = regexp.Match(input)
    If Not match.Success Then Return input
    Dim group As Text.RegularExpressions.Group = match.Groups(group)
    If Not group.Success Then Return input
    Return input.Remove(group.Index, group.Length).Insert(group.Index, replacement)
End Function
+1

- :

Dim evaluator as MatchEvaluator = AddressOf PeriodReplace
Regex.Replace("FIELDCNS 01CM", pattern, evaluator)

Public Function PeriodReplace(match As Match) As String
    Dim replaceTokens As New Regex("(CM|RM)")
    Dim replaceText As String = "04"
    Return match.Groups("NoReplace").Value & _
        replaceTokens.Replace(match.Groups("FirstPeriod").Value, replaceText) & _
        replaceTokens.Replace(match.Groups("LastPeriod").Value, replaceText) & _
        match.Groups("NumberFormat").Value
End Function
0

, , Match, . "id", :

Dim contents = Regex.Replace(contents, "\|(?'id'\d+)\r\n", 
                      Function(m As Match)
                         Return m.ReplaceGroupValue("id", "[REPLACEMENT VALUE]")
                      End Function)

:

<Extension()> _
Function ReplaceGroupValue(ByVal m As Match, ByVal sGroupName$, ByVal sNewValue$) As String
    'get the value of the specified group
    Dim value = m.Groups(sGroupName).Value

    Return m.Value.Replace(value, sNewValue)
End Function

, , :

Dim contents = Regex.Replace(contents, "\|(?'id'\d+)\r\n", 
                      Function(m As Match)
                         Return m.ReplaceGroupValue("id", Function(id) [do something with the id])
                      End Function)

<Extension()> _
Function ReplaceGroupValue(ByVal m As Match, ByVal sGroupName$, ByVal callback As Func(Of String, String)) As String
    'get the value of the specified group
    Dim value = m.Groups(sGroupName).Value

    Return m.Value.Replace(value, callback(value))
End Function

ReplaceGroupValue , , .

0

Source: https://habr.com/ru/post/1713710/


All Articles