Regular Expression Replace Help

G

Guest

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.
 
J

Joshua Flanagan

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;
}
}
 
G

Guest

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 said:
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;
}
}


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.
 
J

Joshua Flanagan

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:
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


:

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;
}
}


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top