?
Solved

Replacing a whole word

Posted on 2011-05-12
11
Medium Priority
?
480 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:yehudaha
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 2

Expert Comment

by:sumeetsuvi
ID: 35745296
Is this u want to done through C# code....i cant get what are u saying to do....
0
 
LVL 9

Expert Comment

by:gery128
ID: 35745301
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
 
LVL 14

Author Comment

by:yehudaha
ID: 35745430
i searching text file and yes i would like an example code in c# (we are in c# zone :-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:gery128
ID: 35745589
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
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 35746426
pls refer to following code at Replace text in a text file

0
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 35746454
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35746980
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
 
LVL 14

Author Comment

by:yehudaha
ID: 35749018
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35749070
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
 
LVL 14

Author Closing Comment

by:yehudaha
ID: 35749173
Thanks a lot !

In case  you know any nice regular expression reference recommendation , i will happy to hear .
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35749592
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question