0

I am trying to find and replace a string when someone inputs it as a search query if they should misspell the code, for example Z0000ECEL is written as Z000ECEL it would replace it to be Z00+ECEL, this is so it finds the closest code to this and find it still even if they misspell it, I am currently using:

        if (Regex.IsMatch(searchWords[0], "^[a-z]+z00+", RegexOptions.IgnoreCase))
        {
            Regex.Replace(searchWords[0], "[0]+", "*0", RegexOptions.IgnoreCase);
        }

I do not want to place a wildcard at the start of the string as this will bring back to many results.

7
  • should the pattern be ^z00+[a-z]+ ? Commented Jun 11, 2012 at 9:03
  • No as the starting letter varies based on the product so I just use ^[a-z]+ Commented Jun 11, 2012 at 9:05
  • in your sample there is Z000ECEL, so at first Z000 and then the rest letters Commented Jun 11, 2012 at 9:06
  • I want it to change Z000 to Z00*ECEL Commented Jun 11, 2012 at 9:09
  • so to simplify - you want to replace the last two zeroes in Z000 to 0*, right? Commented Jun 11, 2012 at 9:15

2 Answers 2

1

Is this doing what you want?

Regex.Replace(searchWords[0], "0{3,}", "00*");

this will replace 3 or more zeros with "00*"

You can also combine this with your first check

Regex.Replace(searchWords[0], "(?<=^[a-z]+z)0{3,}", "00*", RegexOptions.IgnoreCase);

This is involving a lookbehind assertion, so the 0{3,} will be only replaced, if there is a ^[a-z]+z before.

Sign up to request clarification or add additional context in comments.

1 Comment

This is great, thank you (actually turned out I was doing it in the wrong place that would be a lot more complicated)
0

Maybe you are looking for:

str = Regex.Replace(str, "(?i)(^[a-z]+0)0+", "$1+");

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.