473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1212
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 quotedTextFinde rPattern = @"
(?<quote>"")
(?<quotedText>. *?)
\k<quote>
";
const string wordFinderPatte rn = @"(?<word>\s+\w +)";

Regex wordFinder = new Regex(wordFinde rPattern,
RegexOptions.Co mpiled | RegexOptions.Ig norePatternWhit espace);

public string AddAnds(string input)
{
return Regex.Replace(i nput, quotedTextFinde rPattern,
new MatchEvaluator( this.AndInserte r),
RegexOptions.Ig norePatternWhit espace);
}

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

private string IgnoreAnds(Matc h 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.Compar e(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 quotedTextFinde rPattern = @"
(?<quote>"")
(?<quotedText>. *?)
\k<quote>
";
const string wordFinderPatte rn = @"(?<word>\s+\w +)";

Regex wordFinder = new Regex(wordFinde rPattern,
RegexOptions.Co mpiled | RegexOptions.Ig norePatternWhit espace);

public string AddAnds(string input)
{
return Regex.Replace(i nput, quotedTextFinde rPattern,
new MatchEvaluator( this.AndInserte r),
RegexOptions.Ig norePatternWhit espace);
}

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

private string IgnoreAnds(Matc h 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.Compar e(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.Compar e(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 quotedTextFinde rPattern = @"
(?<quote>"" )
(?<quotedText >.*?)
\k<quote>
";
const string wordFinderPatte rn = @"(?<word>\s+\w +)";

Regex wordFinder = new Regex(wordFinde rPattern,
RegexOptions.Co mpiled | RegexOptions.Ig norePatternWhit espace);

public string AddAnds(string input)
{
return Regex.Replace(i nput, quotedTextFinde rPattern,
new MatchEvaluator( this.AndInserte r),
RegexOptions.Ig norePatternWhit espace);
}

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

private string IgnoreAnds(Matc h 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
4157
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) ...
5
2500
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, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a...
6
489
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 with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
3
3202
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert...
4
4823
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 way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace...
2
3006
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 index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and...
7
3802
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way...
9
3346
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 an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular...
3
16910
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 with: import fileinput, string, sys fileQuery = "Text.txt" sourceText = '''SOURCE'''
1
3383
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 "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though:...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.