By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to transpose a word using Regular expression search and replace

P: n/a
Hi,

I want to replace some words in my text files (actually transpose).
For example, I have a whole lot of expressions (words) in my files as
follows:

TABLECUSTOMERS
TABLEORDERS
TABLEORDERLINES
....
....
....

I want to replace them as follows:

CUSTOMERSTABLE
ORDERSTABLE
ORDERLINESTABLE
....
....
....

How can I do this using regular expression search and repalce?

Babu.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ba*****@optonline.net (Babu Mannaravalappil) wrote in
news:35**************************@posting.google.c om:
Hi,

I want to replace some words in my text files (actually
transpose). For example, I have a whole lot of expressions
(words) in my files as follows:

TABLECUSTOMERS
TABLEORDERS
TABLEORDERLINES

I want to replace them as follows:

CUSTOMERSTABLE
ORDERSTABLE
ORDERLINESTABLE

How can I do this using regular expression search and repalce?


Babu,

Use a pattern of "\bTABLE(.*?)\b", and a replacement string of
"$1TABLE".

Here's an example in C#:
using System;
using System.Text.RegularExpressions;

namespace Main
{
public class MainClass
{
[STAThread]
public static int Main()
{
string input = @"
TABLECUSTOMERS
TABLEORDERS
TABLEORDERLINES
";

Console.WriteLine(SwapWords(input, "TABLE"));

return 0;
}

public static string SwapWords(
string input,
string word)
{
return Regex.Replace(input, "\\b" + word + "(.*?)\\b",
"$1" + word, RegexOptions.Singleline);
}
}
}


--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Jul 19 '05 #2

P: n/a
That was quick! Thanks a lot Chris.

Babu.

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:<Xn**********************************@207.46. 248.16>...
ba*****@optonline.net (Babu Mannaravalappil) wrote in
news:35**************************@posting.google.c om:
Hi,

I want to replace some words in my text files (actually
transpose). For example, I have a whole lot of expressions
(words) in my files as follows:

TABLECUSTOMERS
TABLEORDERS
TABLEORDERLINES

I want to replace them as follows:

CUSTOMERSTABLE
ORDERSTABLE
ORDERLINESTABLE

How can I do this using regular expression search and repalce?


Babu,

Use a pattern of "\bTABLE(.*?)\b", and a replacement string of
"$1TABLE".

Here's an example in C#:
using System;
using System.Text.RegularExpressions;

namespace Main
{
public class MainClass
{
[STAThread]
public static int Main()
{
string input = @"
TABLECUSTOMERS
TABLEORDERS
TABLEORDERLINES
";

Console.WriteLine(SwapWords(input, "TABLE"));

return 0;
}

public static string SwapWords(
string input,
string word)
{
return Regex.Replace(input, "\\b" + word + "(.*?)\\b",
"$1" + word, RegexOptions.Singleline);
}
}
}

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.