1

I am trying to replace few characters from a text. These characters maybe from start or end or it may be in middle. The problem is that it hasn't any white space at starting or ending. For example i want to replace "text" from this text with, for instance, "abc".

input: Thisisatextbox

output: Thisisaabcbox

I tried this code so far.

Regex.Replace(textBox.Text, @"\w[text]", "abc");

Any help would be appreciated. Thanks!

0

3 Answers 3

2

Use the following.. [ ] has special meaning (character class) in regex:

Regex.Replace(textBox.Text, @"text", "abc");
Sign up to request clarification or add additional context in comments.

Comments

1

Wouldn't a simple Replace function work? I don't believe Regex is required.
Try this :

string oldText = "Thisisatextbox";
string newText = oldText.Replace("text", "abc");

I believe this would be easier.

Comments

0
Regex.Replace(textBox.Text, @"\w*(text)\w*", "abc");

Use parentheses to capture the text you want to replace.

[] describes a character class. So, if you put [text] it will take ONE character from "t", "x", "e" to search for a possible match.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.