471,066 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 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 1137
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bradley Plett | last post: by
6 posts views Thread by JohnSouth | last post: by
3 posts views Thread by James D. Marshall | last post: by
2 posts views Thread by Brian Kitt | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by leo001 | last post: by

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.