0

I have a problem with replacing a number in a string.

The code is:

Dim Searchstring As String= "2 7 12 33 4 15 22 30 15"
Dim Pattern As String= "^([0-9]$)"
Dim Match As String = Val(TextBox1.Text)
Dim ReplacementString As String = "0"
Dim rgx As Regex = New Regex(pattern)
Dim NewString As String
NewString = Regex.Replace(Searchstring, Match, ReplacementString)
RichTextBox2.Text = NewString

The problem is that when I input a number, say "2", in the textbox and run the program, the replace will be as follows in the richtextbox2.text: 0 7 10 33 4 15 00 30 15 instead of 0 7 12 33 4 15 22 30 15

But when I input the number of "22" or "12" then only these numbers will be replaced and that is okay.

So how can I find a pattern that replaces only the number "2" if I want that and only that, and not change the "12" or "22"?

Please can you help me with this case?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Okko
  • 3
  • 2

4 Answers4

0

The appropriate pattern would be ^([0-9]+) as it would get the first digits and replace them with a 0. Actually, this '$' character represents the end of a string. If you only need to replace the first number, it shouldn't be there.

The '+' character means "One ore more from the previous pattern", then it will search for one or more digits in the "[0-9]" scope from the beginning of the text and capture only the digits.

0

To find any number in the string, you can use \b to denote the beginning or end of a word (i.e., a sequence of letters, digits and underscores).

Example: replace only the number 12:

\b12\b

This will not replace, e.g., in 123 or 412.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

You need to tell it how to delimit the item you want to replace, in this case the "word boundary"* entity \b will work:

Dim searchstring As String = "2 7 12 33 4 15 22 30 15"
Dim match As String = TextBox1.Text
Dim pattern = "\b" & Regex.Escape(match) & "\b"
Dim rgx As Regex = New Regex(pattern)
Dim replacementString As String = "0"
Dim newString = rgx.Replace(searchstring, replacementString)

RichTextBox2.Text = newString

I used Regex.Escape on the search term in case you want to replace something like a . or ?, which have special meanings in regular expressions; escaping them removes the special meaning and uses them as ordinary characters.

You can see that it replaces only the "2" when you enter "2" in the TextBox:

Windows Form showing that only the text "2" has been replaced, but not "22" or "12"

and it replaces both occurrences of "15" when you tell it to:

Windows Forms showing both "15"s have been replaced with "0"

* What is a word boundary in regex?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

While I will not disagree with the answers that have been presented, I will assert that the easiest and most obvious choice would be to use built-in .NET methods to manipulate your string.

Your input is a string that contains numbers delimited by spaces. You then want to replace an exact numeric match with some replacement.

You can do this by splitting the string, looping over the array, modifying the currently iterated items that match your value, and then joining them back:

Dim searchString As String = "2 7 12 33 4 15 22 30 15"
Dim replacementString As String = "0"
If (Not Integer.TryParse(TextBox1.Text, Nothing)) Then
    MessageBox.Show("Please enter a valid integer.")
    Return
End If

Dim searchStringCollection() As String = searchString.Split(" "c)
For index As Integer = 0 To searchStringCollection.Length - 1
    If (searchStringCollection(index) = TextBox1.Text) Then
        searchStringCollection(index) = replacementString
    End If
Next

Dim newString As String = String.Join(" ", searchStringCollection)

Fiddle: https://dotnetfiddle.net/dZfsy6

David
  • 5,877
  • 3
  • 23
  • 40
  • 1
    If you're not using the value from TryParse, you don't need to create a variable for it: you can use `Integer.TryParse(TextBox1.Text, Nothing)` or `Integer.TryParse(TextBox1.Text, New Integer)`. (In C# you'd use a discard: `int.TryParse(TextBox1.Text, _)`.) – Andrew Morton Jan 11 '22 at 18:52
  • @AndrewMorton - true. I've updated my post, thank you. – David Jan 11 '22 at 20:28