Todd Main
Todd Main

Reputation: 29153

Can Regex support not finding something?

I have a string I want to search for two sets of characters. The first one is "in" and the second one is "-in". The phrase to search is "in-in". I want to replace "in" with 8888 and "-in" with 9999, but I can't figure out how to not match the final "in" portion.

This string is actually two substrings, one "in" and the other "-in". I've tried using string.Split, but that isn't helping.

Any thoughts/direction on .NET regex on if this is possible to end up with the string 88889999 based of replacing both in and -in? Right now, all I seem to be getting is 8888-8888

Upvotes: 2

Views: 101

Answers (4)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

You can do this using lookaround assertions:

(?<!-)\bin\b

matches in only if it's not preceded by -.

-in\b

matches -in.

The word boundary anchors make sure that you don't accidentally match bins, into or gain.


Solution:

Sub TestRegExSubstition()
    Dim searchDictionary As New List(Of KeyValuePair(Of String, String))
    searchDictionary.Add(New KeyValuePair(Of String, String)("in", "8888"))
    searchDictionary.Add(New KeyValuePair(Of String, String)("-in", "9999"))

    Dim sentenceArray = {"in-in",
                         "in Parvin-e",
                         "Parvin-in",
                         "in in-e",
                         "Parvin injā-in"}

    For i = 0 To UBound(sentenceArray)
        Dim input As String = sentenceArray(i)
        For Each kvp In searchDictionary
            Dim regex As Regex = New Regex("(?<!-)\b" + kvp.Key + "\b")
            input = regex.Replace(input, kvp.Value)
        Next
        Console.WriteLine(input)
    Next
    Console.ReadLine()
End Sub

Upvotes: 1

venkat balabhadra
venkat balabhadra

Reputation: 537

Basically you want to do following:
1. First search for "in" in the string "str" if found, replace with "8888".
2. Secondly search for "-in" in the string "str" if found, replace with 9999".

There are two solutions to your problem:
Case1 : You know at first that you are going to search for "searchString1" and "searchString2".
Solution: In this case, order your strings in the order of length from largest string to lowest string, then sequentially replace.

Case2 : You don't know at first what is the second string to search for (from your comments it looks like this is the case).
Solution: First form a KEYWORD which is not present in "inputString", replace "searchString1" with KEYWORD, then do a search for "searchString2 or -KEYWORD", replace with your second replace string, then finally replace KEYWORD with your first replace string.

Upvotes: 0

user2648975
user2648975

Reputation: 1

If the expression is strictly always in-in, why are you not using MatchCollection of the Regex ? "(in)(-in)", will let you get Match[1] = in and Match[2] = -in and you can replace them accordingly

Upvotes: 0

Jashaszun
Jashaszun

Reputation: 9270

If your set of substitutions is not prefix-free, then you probably want to consider performing the longer substitutions before the shorter ones. In this case, you would first want to replace "-in" with "9999" and then replace "in" with "8888".

Upvotes: 0

Related Questions