6

I need to replace somethin in a string, but what to replace may vary. It can be

XY - test
XXxY-test
XXyyXx-TEST
yXyy     -Test

and virtually any other combination of whitespaces and cases of the above.

I need to replace the "-test" part and leave the "XXX" alone. So, when using a simple replace

Replace("XXX  -test", "- test", "")

It won't work, obviously. So I need a more sophisticated version of Replace that could handle this task. Is there something I could use or do I have to write it on my own?

1
  • 1
    RegEx(see this) may work for you. I'm not sure what your pattern string would look like, but there's lots of sites for writing those. Commented May 4, 2011 at 13:02

3 Answers 3

8

If you need more flexibility than that provided by mj82's method (for example, you may not know the length of the initial expression), you can use a regular expression. For example:

Regex.Replace("XXX  -test", "\s*-\s*test", "", RegexOptions.IgnoreCase)
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to use your function and am getting an object doesn't exist error. When I add a watch for "Regex" it says that the object is empty. Any ideas why this is happening? Do I need to load a library? I'm in Excel 2010
You need to add Microsoft VBScript Regular Expressions to your references (current version is I think 5.5), then declare the variable using: Dim Regex as New Regexp.
8

This is to compliment eggyal's answer. This is a VBA regexreplace function so that you can use his answer. You'll notice I added ignore_case as an option for flexibility, so your call would be:

=RegexReplace(cell, "\s*-\s*test", "", TRUE)

Here is the function, I hope you find it useful:

' -------------------------------------------------------------------
' Search and Replace using a regular expression
' -------------------------------------------------------------------

Function RegexReplace(ByVal text As String, _
                      ByVal replace_what As String, _
                      ByVal replace_with As String, _
                      Optional ByVal ignore_case As Boolean = False) As String

Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.ignorecase = ignore_case
RE.pattern = replace_what
RE.Global = True
RegexReplace = RE.Replace(text, replace_with)

End Function

Comments

1

If XXX is first, you can use Left(string; 3) function tu cut 3 (or any length you need) letters from left side, then you don't care what's after it.

2 Comments

Okay, I forgot to clarify that in the question. XXX is also variable of any character (maybe even including "test" and alike)
If you can cut until "-" sign (and it cannot be in XXX expression), then this may help: Left(mystr, InStr(mystr, "-") - 1), it cuts all the letters until first occurance of "-". Otherwise - Regex is best solution.

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.