473,395 Members | 1,688 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,395 software developers and data experts.

regex -- substitute chars outside quoted strings

What I want to do sounds simple, but it's defeating me. I want to
substitute all occurences of a colon : character in a string with an @
character -- unless the : occurs within a single or double-quoted
substring. Surely this can be done with regular expressions? Any regex
gurus know how to do it?
Jul 21 '05 #1
5 2117
ga**@mccull.org (Gary McCullough) writes:
What I want to do sounds simple, but it's defeating me. I want to
substitute all occurences of a colon : character in a string with an @
character -- unless the : occurs within a single or double-quoted
substring. Surely this can be done with regular expressions? Any regex
gurus know how to do it?


Preprocess the string: split it into the parts with quotes and without.

Lexer would work great in this case.
Jul 21 '05 #2
As a human being, this seems like a very simple problem, but trying to get a
computer to understand what you want is another story. Let's look at an
example of why this is more complex of a problem, using the following string:

Meeting today : 10AM
This is an "example: 1"
Meeting tomorrow : 11AM
This is another "example: 2"

If I understand the requirements, the desired output should be:

Meeting today @ 10AM
This is an "example: 1"
Meeting tomorrow @ 11AM
This is another "example: 2"

Unfortunately, if we wrote a regular expression to replace any colon (:) not
inside quotes, the colon before 11AM would not be changed, because there is a
preceding and following quote.

I've had a similar problem before as well, and the best solution I could
think of was to extract all of the quoted strings and replace them with an
escape sequence, then do the replacement, then re-inflate the escape
sequences with the extracted values.

An example would look something like:

string s = @"
Meeting today : 10AM
This is an ""example: 1""
Meeting tomorrow : 11AM
This is another ""example: 1""
";

// Extract the quoted strings
MatchCollection matches = Regex.Matches(s, @"""[^\""]+?""");
for(int x=matches.Count-1; x>-1; x--)
{
Match match = matches[x];
s = s.Remove(match.Index, match.Length);
s = s.Insert(match.Index, "{" + x + "}");
}

// Replace the remaining : with @
s = s.Replace(':', '@');

// Reinflate the escaped strings
for(int x=0; x<matches.Count; x++)
{
Match match = matches[x];
s = s.Remove(match.Index, x.ToString().Length + 2);
s = s.Insert(match.Index, match.Value);
}
If anyone else has a better solution, I'd love to hear it.

Hope this helps.

--
Jason Whitted
Jul 21 '05 #3
Jason,

I was afraid you'd say that.

Your analysis of the problem is dead on. In reality I'm converting
parameterized sql statements from SqlServer format to Oracle format and
vice versa (thus substituting :'s and @'s), but your example works just
as well. I'm surprised this is such a hard problem.

Since I can't figure out how to do it with a regex I'm just using
regex's to extract the literals and doing replaces on the other bits.

----------------------
Gary McCullough
http://mccull.org/gary

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #4
Jason,

I was afraid you'd say that.

Your analysis of the problem is dead on. In reality I'm converting
parameterized sql statements from SqlServer format to Oracle format and
vice versa (thus substituting :'s and @'s), but your example works just
as well. I'm surprised this is such a hard problem.

Since I can't figure out how to do it with a regex I'm just using
regex's to extract the literals and doing replaces on the other bits.

----------------------
Gary McCullough
http://mccull.org/gary

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #5
On 9 Nov 2004 08:47:48 -0800, Gary McCullough wrote:
What I want to do sounds simple, but it's defeating me. I want to
substitute all occurences of a colon : character in a string with an @
character -- unless the : occurs within a single or double-quoted
substring. Surely this can be done with regular expressions? Any regex
gurus know how to do it?


This pattern will find all occurrences of a particular charcter except
where it occurs between quotation marks.

This example finds the occurrences of the colon character except where it
appears within quotation marks. If you want single quotes, then change the
\x22 to \x27

Dim sPattern As String = ":(?=([^\x22]*\x22[^\x22]*\x22)*(?![^\x22]*\x22))"
Dim sInput As String = "Meeting tomorrow : 11AM ""Example: 1"""

Dim mc As New MatchCollection =
Regex.Matches(sInput,sPattern,RegExOption.Explicit Capture)

The match collection here should contain only one item the location of the
colon after the word tomorrow.

If you use the replace function:

Dim sResult As string = Regex.Replace(sInput, sPattern, "@")
'sResult = "Meeting tomorrow @ 11AM "Example: 1""

Perhaps this will help you.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #6

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

Similar topics

4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
5
by: Gary McCullough | last post by:
What I want to do sounds simple, but it's defeating me. I want to substitute all occurences of a colon : character in a string with an @ character -- unless the : occurs within a single or...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
14
by: tom t/LA | last post by:
Here is a function to convert a CSV file to a Javascript array. Uses idealized file reading functions based on the std C library, since there is no Javascript standard. Not fully tested. ...
9
by: Simon Woods | last post by:
Hi I'm new to Regular Expressions so ... I trying to work out regular expressions to parse the following (a + (b + c)) I really want to replace it with
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.