Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
searching elements are one, two, three.......twenty

these elements are available in different possible ways.
one_two
one_two_three
one_two_three_four
one]two[three-four

the expected output is
one-two
one-two-three
one-two-three-four
one-two-three-four

it means all special symbol replace by (-) hyphen,
any possible to get answer
Posted

Try below code.

C#
string str = "one]two[three-four";
str = Regex.Replace(str, "[^0-9a-zA-Z]+", "-");
 
Share this answer
 
Comments
George Jonsson 17-Dec-14 4:22am    
Only covers the English alphabet.
You can try to use Regex.Replace
See Regex.Replace Method[^]

C#
class Program
{
    static void Main(string[] args)
    {
        string s1 = "one_two";
        string s2 = "one_two_three";
        string s3 = "one_two_three_four";
        string s4 = "one]two[three-four";

        [UPDATE]
        Regex regex = new Regex(@"[_\[\]]");
        Regex regex = new Regex(@"[\S\s]");

        string result = "";
        result = regex.Replace(s1, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s2, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s3, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s4, new MatchEvaluator(Program.ReplaceWithHyphen));

    }

    [UPDATE]
    static string ReplaceWithHyphen(Match m)
    {
        // Return the hyphen as a replacement if the character is not a letter or digit
        char ch = m.Value[0];
        if (Char.IsLetterOrDigit(ch))
            return ch.ToString();
        else
            return "-";
    }
}
 
Share this answer
 
v3
Comments
smksamy 17-Dec-14 1:50am    
if document contains 100 possible terms, we cant write 100 possible statements , we cant perdict
Praveen Kumar Upadhyay 17-Dec-14 1:54am    
correct. This is not a good way of programming. Check out my answer, it will replace any spacial character with hyphen.
George Jonsson 17-Dec-14 4:16am    
It depends on the exact requirement, but you have a good point.
But if all characters but digits and letters should be replaced by a hyphen it would be cumbersome with my approach.
However, your solution only covers letters in the English alphabet, so if you have a text that contains any Nordic or German language and many others, special letters like å, ä, ö, æ, Ü etc. would also be replaced by a hyphen.
This is not a good way of programming either.
See my updated solution.
Regex + Linq solution:
C#
List<string> words = new List<string>{"one_two",
            "one_two_three",
            "one_two_three_four",
            "one]two[three-four"};


var qry = words.Select(w=>w = new Regex(@"(\])|(\[)|(_)").Replace(w,"-"));


Result:
one-two
one-two-three
one-two-three-four
one-two-three-four
 
Share this answer
 
Something like this should work:

C#
static void Main(string[] args)
{
    string[] searchElemnts = new string[] { "öne", "two","three", "four" };
    string[] samples = new string[] { "öne_two", "öne_two_three","öne_two_three_four", "öne]two[three-four" };

    string[] copySamples = samples.ToArray();

    for (int i = 0; i < copySamples.Length; i++)
    {
        foreach (string element in searchElemnts)
        {
            copySamples[i] = copySamples[i].Replace(element, "");
        }
    }

    char[] unwantedChars = string.Join("",copySamples.ToArray()).ToCharArray().Distinct().ToArray();
    Regex regex = new Regex("[" + Regex.Escape(new string(unwantedChars)).Replace("]", "\\]") + "]");

    string result = "";
    for (int i = 0; i < samples.Length; i++)
    {
        result = regex.Replace(samples[i], "-");
        System.Diagnostics.Debug.Print(result);
    }
}


What this code do is:

-Anything other that searched elements are assumed as unwanted character.
-Prepares a list of unwanted characters,
-Replace each unwanted character with "-".

Hope that helps!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900