RegExpr : Find and Replace last occurrence of a space(ASCII 20) in a line of text

Zum Alles,

Given the string:
"xxxxx x xxxxxxxxxx","nnnn xxxxxxxxxxxxxxx","xxxxxxx xx nnnnn/lf
I want to find and replace the last occurence of '  ' in the line of text with a ", to give me the following:
"xxxxx x xxxxxxxxxx","nnnn xxxxxxxxxxxxxxx","xxxxxxx xx "nnnnn/lf

This REGEX " ^*\s " finds the first space in line.  I can not figure out a REGEX to find the last space in the line.

Any help would be appreciated.

Thanks

Mike
mhottoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl BohmanCommented:
In Perl regex:
s/(.*) /$1"/

Open in new window

0
Carl BohmanCommented:
Alternately:
s/ (?=[^ ]*$)/"/

Open in new window

0
Carl BohmanCommented:
Note also that "\s" in your original regex matches any whitespace character (space, tab, etc.), not just a regular space.
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

ghostdog74Commented:

$\ = "\n";              
while (<>) {
    chomp;      # strip record separator
    @f = split(/\s+/, $_);
    $f[-1] = " \"" . $f[-1];
    print join($,,@f);
}

Open in new window

0
Adam314Commented:
ghostdog74 - that also has the side effect of replacing all groups of spaces with $,.  Even if $, is set to " ", you still replace multiple spaces with a single space.
0
mhottoAuthor Commented:
To all:  For learning/instructional/testing purposes, I am using The Regex Coach - interactive regular expressions(The Regex Coach is Copyright © 2003-2004 Dr. Edmund Weitz - All Rights Reserved. http://weitz.de/regex-coach/) to test solutions presented.  Using REGEX COACH, I have been unable to just find the last space in the line.

I need to simply my initial problem request to two parts.

First part of question, and most significant part.

1)Find last space in line of text.
2)Replace that single space with a single "

Thanks

Mike

0
mhottoAuthor Commented:
Zum Alles:  Test Example.
"Xxxxx X Xx-xxxxxxx","3037 RANDOLPH STREET","Xxxxxxx NE 68510
This REGEX
\s.....$
  highlighs/finds

 68510
I need to find/highlight only the space before the 6.  Or more generalized.  The final space in text string.

Viel Dank

Mike
0
Carl BohmanCommented:
This works for me in Regex Coach.  Note that there is a sinle space character before the open paren and nothing after the closing paren.
 (?=[^ ]*$)

Open in new window

0
mhottoAuthor Commented:
Bounsy,

Points are yours.  You answered my primary question, but before I close this out.  Is it possible to find the space using
    \Z  Match only at end of string, or before newline at the end
    \z  Match only at end of string
Also, using REGEX COACH, what is the replace code

to replace space w "

Thanks

Mike
0
Carl BohmanCommented:
The replacement string is just a single double quote character.

As for \Z or \z, they serve similar functions to the $ token.  Which one you use really depends on your data and what your regex modifiers are.  In most cases (simple, single-line strings with an optional carriage return at the end and no embedded carriage returns in the middle), the $ is used and does what you are looking for.  Regex Coach is great for helping you see what happens if your Target string contains multiple lines, what happens if the "m" or "s" regex modifiers are turned on, etc.

One thing you might also want to try (if you haven't already) is the Step functionality in Regex Coach that allows you to see exactly how the computer "executes" your regular expressions.  It can be eye-opening at times to see how efficient or inefficient some regular expressions can be.  It also can help you build a regular expression if you can see where the one you were working on strays from the behavior you are after.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mhottoAuthor Commented:
Thanks, Bounsy.  Appreciate your taking time to explain it.

Mike
0
mhottoAuthor Commented:
Bounsy, provided the specific solution I, required, with the same tool I, was using.  Bounsy, also added additional explanation.  Excellent Response.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.