Cash Talks - BS Walks RSS 2.0
# Saturday, 01 January 2011

I had an interesting problem where I needed to protect text within Quoted Strings from a series of Regex search and replace operations.  Trying to write even simple regular expressions replace process that would exclude text in quotes was too painful for my understanding of Regex.  I’m sure there are gurus who can throw that out but I’m not one of them. 

While my job was more extensive than simply upper case, what if you wanted to change all characters in a string to upper case except for ones within double quotes?  For example:

The “quick brown fox” jumped over the “lazy dog”
would become
THE “quick brown fox” JUMPED OVER THE “lazy dog”

There are number of quoted string regex expressions out there and I needed one that provided for escaped “ within the string that had a preceding  \.

The following regex replace would replace quoted strings with the word STUFF

[regex]::Replace($value,'"([^"\\]*(\\.[^"\\]*)*)"', 'STUFF')

Not much help but that’s where the regex MatchEvaluator comes in to play.  It’s a function that gets called for each match and the text it returns is what gets used.  For example, what if I wanted to upper case only characters within double quotes?  The following code would make quick work of it.

$value = 'The "quick brown fox" jumped over the "lazy dog"'

 

$QuotedTextMatchEvaluator = [System.Text.RegularExpressions.MatchEvaluator]{

       $args[0].ToString().ToUpper()

}

 

[regex]::Replace($value,'"([^"\\]*(\\.[^"\\]*)*)"',$QuotedTextMatchEvaluator)

This produces
The “QUICK BROWN FOX” jumped over the “LAZY DOG”

Simple enough but as I said it was the opposite of what I needed and I was doing more interesting work than generating uppercase.  My idea was to grab all of the quoted strings, save them in an array and then restore them when I was done.  The save-quotedstrings function replaces each quoted string with a unique token then restore-quotedstrings replaces the token with the saved string.  While context will dictate what is possible as a token, I chose to use an ASCII 1, a rarely used non-visible character followed by an incrementing number for each quoted string.  save-quotestrings returns the tokenized string and the array of saved quoted strings.  restore-quotedstrings takes a tokenized string and the saved string array and restores them.

 

function save-quotedstrings([string]$value)

{

       $SavedStrings = @()

       $QuotedTextMatchEvaluator = [System.Text.RegularExpressions.MatchEvaluator]{

              $SavedStrings += $args[0].ToString()

              [char]1 + ($SavedStrings.Count)

       }

       [regex]::Replace($value,'"([^"\\]*(\\.[^"\\]*)*)"',$QuotedTextMatchEvaluator),$SavedStrings

}

 

function restore-quotedstrings([string]$value,[array]$savedStrings)

{

       (1..$savedStrings.Count) | %{$value = $value.Replace([char]1 + "$_",$savedStrings[$_-1])}

       $value

}

 

 

$value = 'This "is \"a\" test" of a "quoted string" saver'

$value

$value,$savedStrings = save-quotedstrings $value

$value

$value = $value.ToUpper()

$value

$value = restore-quotedstrings $value $savedStrings

$value

The preceding code generates the following output.

This "is \"a\" test" of a "quoted string" saver
This _1 of a _2 saver
THIS _1 OF A _2 SAVER
THIS "is \"a\" test" OF A "quoted string" SAVER

Note:  the _ is show instead of the non-displayable ASCII 1.

Saturday, 01 January 2011 00:16:06 (GMT Standard Time, UTC+00:00)  #    Comments [55] -
Powershell
Friday, 07 January 2011 07:47:21 (GMT Standard Time, UTC+00:00)
nicely done
Friday, 12 December 2014 10:47:33 (GMT Standard Time, UTC+00:00)
hello there and thanks on your inomofatirn ? I have certainly picked up something new from right here. I did then again expertise a few technical points using this web site, as I skilled to reload the site lots of times prior to I could get it to load properly. I have been brooding about if your web hosting is OK? Not that I'm complaining, but sluggish loading instances instances will very frequently affect your placement in google and could harm your high-quality score if ads and marketing with Adwords. Well I'm including this RSS to my email and could look out for a lot extra of your respective intriguing content. Make sure you update this once more soon..
Tuesday, 03 March 2015 17:10:23 (GMT Standard Time, UTC+00:00)
That's an apt answer to an interesting question
Wednesday, 04 March 2015 17:51:22 (GMT Standard Time, UTC+00:00)
It's spooky how clever some ppl are. Thanks!
Friday, 13 March 2015 21:49:17 (GMT Standard Time, UTC+00:00)
Smack-dab what I was looking for-ty!
Monday, 16 March 2015 07:36:19 (GMT Standard Time, UTC+00:00)
Holy shiznit, this is so cool thank you.
Monday, 23 March 2015 06:50:42 (GMT Standard Time, UTC+00:00)
How could any of this be better stated? It couldn't.
Thursday, 26 March 2015 04:24:37 (GMT Standard Time, UTC+00:00)
Son of a gun, this is so helpful!
Monday, 06 April 2015 13:36:45 (GMT Daylight Time, UTC+01:00)
Begun, the great internet education has.
Monday, 13 April 2015 16:42:15 (GMT Daylight Time, UTC+01:00)
That's a posting full of insight!
Monday, 20 April 2015 01:39:48 (GMT Daylight Time, UTC+01:00)
No more s***. All posts of this quality from now on
Tuesday, 21 April 2015 04:29:37 (GMT Daylight Time, UTC+01:00)
This makes everything so completely painless.
Wednesday, 22 April 2015 12:24:09 (GMT Daylight Time, UTC+01:00)
You've really helped me understand the issues. Thanks.
Thursday, 30 April 2015 11:51:03 (GMT Daylight Time, UTC+01:00)
The forum is a brighter place thanks to your posts. Thanks!
Saturday, 02 May 2015 20:02:28 (GMT Daylight Time, UTC+01:00)
It's wonderful to have you on our side, haha!
Saturday, 09 May 2015 04:31:47 (GMT Daylight Time, UTC+01:00)
Deep thinking - adds a new dimension to it all.
Saturday, 09 May 2015 09:30:02 (GMT Daylight Time, UTC+01:00)
Good to see real expertise on display. Your contribution is most welcome.
Tuesday, 19 May 2015 15:15:08 (GMT Daylight Time, UTC+01:00)
Unparalleled accuracy, unequivocal clarity, and undeniable importance!
Sunday, 24 May 2015 21:59:41 (GMT Daylight Time, UTC+01:00)
Whoever edits and publishes these articles really knows what they're doing.
Monday, 25 May 2015 16:13:14 (GMT Daylight Time, UTC+01:00)
This forum needed shaking up and you've just done that. Great post!
Monday, 08 June 2015 16:31:25 (GMT Daylight Time, UTC+01:00)
It's wonderful to have you on our side, haha!
Saturday, 20 June 2015 18:21:17 (GMT Daylight Time, UTC+01:00)
Why do I bother calling up people when I can just read this!
Sunday, 21 June 2015 16:23:35 (GMT Daylight Time, UTC+01:00)
Great article, thank you again for writing.
Monday, 22 June 2015 21:49:07 (GMT Daylight Time, UTC+01:00)
Ah, i see. Well that's not too tricky at all!"
Friday, 26 June 2015 12:58:48 (GMT Daylight Time, UTC+01:00)
With all these silly websites, such a great page keeps my internet hope alive.
Tuesday, 04 August 2015 13:43:35 (GMT Daylight Time, UTC+01:00)
A really good answer, full of rationality!
Thursday, 30 March 2017 18:15:03 (GMT Daylight Time, UTC+01:00)
Meet Brisbane Mature Singles for Dating

Dating Relationship Love

Free Local Personals

International delight creamers in singles women

http://adultdatingbrisbane.com/
http://datingice.com/
http://rhdating.com/
http://sexdatingdelight.com/
Wednesday, 12 April 2017 17:43:34 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 17:45:00 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 17:55:09 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 17:58:52 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 18:05:18 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 21:42:32 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 21:53:44 (GMT Daylight Time, UTC+01:00)
Wednesday, 12 April 2017 22:01:37 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 13:07:38 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 13:08:32 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 13:08:54 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 16:09:44 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 16:12:46 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 21:19:35 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 21:44:28 (GMT Daylight Time, UTC+01:00)
Thursday, 13 April 2017 21:49:49 (GMT Daylight Time, UTC+01:00)
Friday, 14 April 2017 13:04:21 (GMT Daylight Time, UTC+01:00)
Friday, 14 April 2017 18:24:36 (GMT Daylight Time, UTC+01:00)
Friday, 14 April 2017 22:03:32 (GMT Daylight Time, UTC+01:00)
Friday, 14 April 2017 22:19:27 (GMT Daylight Time, UTC+01:00)
Saturday, 15 April 2017 11:01:43 (GMT Daylight Time, UTC+01:00)
Saturday, 15 April 2017 12:33:48 (GMT Daylight Time, UTC+01:00)
Saturday, 15 April 2017 19:19:50 (GMT Daylight Time, UTC+01:00)
Saturday, 15 April 2017 20:35:08 (GMT Daylight Time, UTC+01:00)
Saturday, 15 April 2017 23:23:41 (GMT Daylight Time, UTC+01:00)
Wednesday, 19 April 2017 20:00:06 (GMT Daylight Time, UTC+01:00)
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Archive
<2017 April>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Cash Foley
Sign In
Statistics
Total Posts: 20
This Year: 0
This Month: 0
This Week: 0
Comments: 5994
Themes
Pick a theme:
All Content © 2017, Cash Foley
DasBlog theme 'Business' created by Christoph De Baene (delarou)