Trying to use Regex for replacing specific word?
Hello, I have been reading up on Regex and have a hard time understanding it. I am trying to make this feature to remove a certain word the user might want to remove from the list(word is the variable) but from what I have been reading up about, they have the set word already written in the code and the length of the word to search for. I could have these three words, Hello | HelloWorld | HelloUniverse | and when I type Hello, it removes Hello from all the words leaving World and Universe. I do not know how to use Regex to find that standalone word and I do not fully understand how to use Regex. Sorry for asking how to do this but I don't know where else to ask for help.
Re: Trying to use Regex for replacing specific word?
Regex is a small programming language. The language's intent is to define patterns of text. To that end, it has several ways to define "character classes", which is basically "more than one character that matches a certain category".
\b stands for "word boundary". What it actually means is pretty complex, but for the most part it matches "the space in between words".
So a pattern for the word "Hello" that would match "Hello World" but not "HelloWorld" would look like:
If you use that pattern with Replace(), you will also replace the boundary. That is to say, "foo Hello World" would become "foo World" instead of "foo World" with two spaces. Maybe you want that, maybe you don't, it'll be more complicated if that's not what you want. My hunch is you want it.
Re: Trying to use Regex for replacing specific word?
What I mean is this: 3 words: Hello, HelloWorld, HelloUniverse. The user chooses to remove the word Hello and only Hello. When I do that it removes all three hellos leaving just World and Universe. I want it to where when that user clicks remove it only removes the word hello leaving HelloWorld and HelloUniverse as a result and I can not figure out a way to do that. That's why I came and asked the higher knowledge users
Re: Trying to use Regex for replacing specific word?
Did you even try my advice?
Re: Trying to use Regex for replacing specific word?
Yeah I did but I think I did it wrong honestly, when I did it this way, it replaced hello with /b. Here is the code I did it with:
Code:
rtbData.Text = rtbData.Text.Replace(remove, "\b")
I then tried this
Code:
Dim regex As Regex = New Regex(rtbData.Text)
regex.Replace(remove, "")
Remove equals the variable of the word the user want's removed
Re: Trying to use Regex for replacing specific word?
I meant use the Regex "Hello\b". Not "\b".
"Hello\b" tells Regex "Match the characters H, e, l, l, o, and anything that counts as a word boundary." What you did instead is tell VB, "Please replace the pattern "Hello" with the string "\b".
Re: Trying to use Regex for replacing specific word?
Yeah I did that. I think I am missing something. Here is the code:
Code:
\
Dim regex As Regex = New Regex(rtbData.Text)
regex.Replace("Hello\b", "")
Now I know I am new to Regex but at "Hello\b", it is looking for Hello\b and there isn't any Hello\b. I did although try this:
Code:
Dim regex As Regex = New Regex(rtbData.Text)
regex.Replace(remove + "\b", "")
that didn't work but in some examples of people using Regex, they did that. They were not using the replace but I figured it all worked the same way.
Re: Trying to use Regex for replacing specific word?
OK, let's go over the steps.
At the core of a Regex is "the pattern". That's the symbols that tell it what it is matching. When you create the Regex, you give it the pattern:
Code:
Dim r As New Regex("Hello\b")
In your code, you're giving it the whole text as the pattern. That'll just match all of the text.
Regex.Replace() takes two parameters: the text that will be scanned and the text you would like to replace matches. The pattern is already part of the Regex, so you don't have to specify it again.
Code:
r.Replace(rtbData.Text, "")
See also the MSDN documentation.