Replacing a whole word

hey

how do i search and replace a whole word , what i mean:

i want to replace only the left or right words like this line:

float: left

BUT NOT IN LINES LIKE THIS:

margin-right: auto;

"standalone" right otr left :-)

thanks
LVL 14
yehudahaAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Wow...  I'm getting sloppy  : \

You're correct. I forgot the replacement (and the closing parentheses). The correct code is:
string toFind = "left";
string data = "float: left";
string result = System.Text.RegularExpressions.Regex.Replace(data, @"\b(?<!-)" + toFind + @"(?!-)\b", "center"); // for example

Open in new window

0
 
sumeetsuviCommented:
Is this u want to done through C# code....i cant get what are u saying to do....
0
 
gery128Commented:
what is the source? is it a string or string array (words in each array element)?

You can use regular expression to check if that particular word present or not and to omit the word with character '-' in it.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
yehudahaAuthor Commented:
i searching text file and yes i would like an example code in c# (we are in c# zone :-)
0
 
gery128Commented:
you can try following code:

string input = null;
FileStream FS = new FileStream("Text file with path", FileMode.Open, FileAccess.Read, FileShare.Read);
        StreamReader SR = new StreamReader(FS);

        while ((input = SR.ReadLine()) != null)
        {
            if(input.Contains(":"))
            {
                string[] myWords = input.Split(' ');
                for(int i=0;i<myWords.Length;i++)
                {
                    if (myWords[i].EndsWith(":") && myWords[i].Contains("-") == false)
                    { 
                        //do something with next and previous word of ":"
                        //like myWords[i] for current word "float:" itself & myWords[i+1] for next word "left"
                    }
                }
            }
}

Open in new window

0
 
Anurag AgarwalCommented:
pls refer to following code at Replace text in a text file

0
 
Anurag AgarwalCommented:
using System;
using System.IO;
using System.Text.RegularExpressions;

/// <summary>
/// Replaces text in a file.
/// </summary>
/// <param name="filePath">Path of the text file.</param>
/// <param name="searchText">Text to search for.</param>
/// <param name="replaceText">Text to replace the search text.</param>
static public void ReplaceInFile( string filePath, string searchText, string replaceText )
{
    StreamReader reader = new StreamReader( filePath );
    string content = reader.ReadToEnd();
    reader.Close();

    content = Regex.Replace( content, searchText, replaceText );

    StreamWriter writer = new StreamWriter( filePath );
    writer.Write( content );
    writer.Close();
}

Open in new window

0
 
käµfm³d 👽Commented:
Try the following pattern for a regex replacement, which should only examine whole words, but exclude things like:

    left-justified
    sharp-left turn

If you don't care about scenarios like the above, then you can remove everything inside and including the parentheses in the below.
string toFind = "left";
string data = "float: left";
string result = System.Text.RegularExpressions.Regex.Replace(data, @"\b(?<!-)" + toFind + @"(?!-)\b";

Open in new window

0
 
yehudahaAuthor Commented:
hey kaufmed

thanks for your answer.

i'm getting : Error      1      An object reference is required for the non-static field, method, or property 'System.Text.RegularExpressions.Regex.Replace(string, string)'

i notice there isn't a replacement string in the code, i mean with what i want to replace the left.
maybe thats the problem ?

0
 
yehudahaAuthor Commented:
Thanks a lot !

In case  you know any nice regular expression reference recommendation , i will happy to hear .
0
 
käµfm³d 👽Commented:
In case  you know any nice regular expression reference recommendation , i will happy to hear .
My site of preference is www.regular-expressions.info. I also like the .NET-based tool Expresso for building expressions. It has a nice feature in that it will explain what parts of your pattern are doing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.