1

Given a string like this: Fixing this [HFW] takes alot of [HFW] time [HFW]

should be this: [CW] [CW] [HFW] [CW] [CW] [CW] [HFW] [CW] [HFW]

I need to replace any word with [CW] except the tags [HFW]. I've been trying this alot, the best i got is:

regex = new Regex(@"[^(\[HFW\])_]+");
cleanText = regex.Replace(cleanText, " [CW] ");

Which almost works, but i get the first capital F left out, being a part of [HFW] tag. I mean, it won't take [HFW] as a group.

Thank you

1
  • Why regex? Isn't this easier to do by tokenizing the string? Commented May 13, 2013 at 17:32

1 Answer 1

2

I've got one which is a bit... cranky? But it does the trick for your sample string:

(?<!\[|F|\[F)\w+(?!W|\]|W\]\])

The issue with [^...] is that it doesn't look at the order of the characters \[, H, F, W or \].

There's probably a more elegant one than this and I'm still trying to look for it.

Another possible one:

\w+(?=\s)|(?<=\s)\w+
Sign up to request clarification or add additional context in comments.

5 Comments

That's not working, it's replacing every character with [CW] except [HFW]. I need to replace every whole word.
@user569605 It's replacing every whole word, I already tested it here. Your current pattern replaces series of words by a single [CW]. Is that what you actually want?
I found another expression: \w+(?=\s)|(?<=\s)\w+. Maybe that one is better at your code? I'm still confused that C# is taking every character :(
It was my mistake, it takes the whole word. Thank you alot :)
@user569605 Awesome then! :)

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.