473,320 Members | 1,950 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,320 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 2109
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.