473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regular Expression Replace Help

Hi

I'm having some troubles getting my regex to work. I have a string as follows
The "quick and brown" fox "jumped over the" lazy dog.

The output should be as follows:
The "quick and brown" fox "jumped and over and the" lazy dog.

So the expression needs to insert 'and' between the groups of words enclosed
in double quotes, ignoring the insert if the 'and' word exists bewteen the
quotes.

If anyone has some ideas how to do this, I would be very greatful.
Nov 22 '05 #1
3 1201
I wasn't able to do it in one pass, but this solution seems to work:

// call the AddAnds() method with your input text
const string quotedTextFinderPattern = @"
(?<quote>"")
(?<quotedText>.*?)
\k<quote>
";
const string wordFinderPattern = @"(?<word>\s+\w+)";

Regex wordFinder = new Regex(wordFinderPattern,
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

public string AddAnds(string input)
{
return Regex.Replace(input, quotedTextFinderPattern,
new MatchEvaluator(this.AndInserter),
RegexOptions.IgnorePatternWhitespace);
}

private string AndInserter(Match match)
{
string quotedText = match.Groups["quotedText"].Value;
return "\"" +
wordFinder.Replace(quotedText,
new MatchEvaluator(this.IgnoreAnds))
+ "\"";
}

private string IgnoreAnds(Match match)
{
string word = match.Groups["word"].Value;
if (String.Compare(word, " and", true) != 0)
{
return " and" + word;
}
else
{
return String.Empty;
}
}

Craig wrote:
Hi

I'm having some troubles getting my regex to work. I have a string as follows
The "quick and brown" fox "jumped over the" lazy dog.

The output should be as follows:
The "quick and brown" fox "jumped and over and the" lazy dog.

So the expression needs to insert 'and' between the groups of words enclosed
in double quotes, ignoring the insert if the 'and' word exists bewteen the
quotes.

If anyone has some ideas how to do this, I would be very greatful.

Nov 22 '05 #2
Hi Joshua

Thanks for the great sample.

I played with your code to include an 'or' as a word to disregard.

Example Input:
The "lazy or brown dog" just "plays and eats sleeps" in the "green meadow
butterfly" watching

Excepted Output:
The "lazy or brown and dog" just "plays and eats and sleeps" in the "green
and meadow and butterfly" watching

I modified the if...else section of IgnoreAnds function to the following but
it didn't change anything:
if ((String.Compare(word, " and", true) != 0) || (String.Compare(word, "
or", true) != 0)) {
return " and" + word;
}

What would you suggest?

Craig
"Joshua Flanagan" wrote:
I wasn't able to do it in one pass, but this solution seems to work:

// call the AddAnds() method with your input text
const string quotedTextFinderPattern = @"
(?<quote>"")
(?<quotedText>.*?)
\k<quote>
";
const string wordFinderPattern = @"(?<word>\s+\w+)";

Regex wordFinder = new Regex(wordFinderPattern,
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

public string AddAnds(string input)
{
return Regex.Replace(input, quotedTextFinderPattern,
new MatchEvaluator(this.AndInserter),
RegexOptions.IgnorePatternWhitespace);
}

private string AndInserter(Match match)
{
string quotedText = match.Groups["quotedText"].Value;
return "\"" +
wordFinder.Replace(quotedText,
new MatchEvaluator(this.IgnoreAnds))
+ "\"";
}

private string IgnoreAnds(Match match)
{
string word = match.Groups["word"].Value;
if (String.Compare(word, " and", true) != 0)
{
return " and" + word;
}
else
{
return String.Empty;
}
}

Craig wrote:
Hi

I'm having some troubles getting my regex to work. I have a string as follows
The "quick and brown" fox "jumped over the" lazy dog.

The output should be as follows:
The "quick and brown" fox "jumped and over and the" lazy dog.

So the expression needs to insert 'and' between the groups of words enclosed
in double quotes, ignoring the insert if the 'and' word exists bewteen the
quotes.

If anyone has some ideas how to do this, I would be very greatful.

Nov 22 '05 #3
I see from your other post that you found a solution that works. Great!

I assume this question isn't relevant anymore, but just in case, the
problem was in your boolean logic:

Craig wrote:
if ((String.Compare(word, " and", true) != 0) || (String.Compare(word, "
or", true) != 0)) {
return " and" + word;
}
If the word is " or", the first part of the expression will evaluate to
TRUE, so the second part is never evaluated. Only 1 part of an OR
expression needs to be TRUE to make the entire expression TRUE. If you
need them both to be true, you would use an AND.

if ((String.Compare(word, " and", true) != 0) && (String.Compare(word, "
or", true) != 0))


What would you suggest?

Craig
"Joshua Flanagan" wrote:

I wasn't able to do it in one pass, but this solution seems to work:

// call the AddAnds() method with your input text
const string quotedTextFinderPattern = @"
(?<quote>"")
(?<quotedText>.*?)
\k<quote>
";
const string wordFinderPattern = @"(?<word>\s+\w+)";

Regex wordFinder = new Regex(wordFinderPattern,
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

public string AddAnds(string input)
{
return Regex.Replace(input, quotedTextFinderPattern,
new MatchEvaluator(this.AndInserter),
RegexOptions.IgnorePatternWhitespace);
}

private string AndInserter(Match match)
{
string quotedText = match.Groups["quotedText"].Value;
return "\"" +
wordFinder.Replace(quotedText,
new MatchEvaluator(this.IgnoreAnds))
+ "\"";
}

private string IgnoreAnds(Match match)
{
string word = match.Groups["word"].Value;
if (String.Compare(word, " and", true) != 0)
{
return " and" + word;
}
else
{
return String.Empty;
}
}

Craig wrote:
Hi

I'm having some troubles getting my regex to work. I have a string as follows
The "quick and brown" fox "jumped over the" lazy dog.

The output should be as follows:
The "quick and brown" fox "jumped and over and the" lazy dog.

So the expression needs to insert 'and' between the groups of words enclosed
in double quotes, ignoring the insert if the 'and' word exists bewteen the
quotes.

If anyone has some ideas how to do this, I would be very greatful.

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.