Link to home
Start Free TrialLog in
Avatar of mhotto
mhottoFlag for United States of America

asked on

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
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

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

Open in new window

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

Open in new window

Note also that "\s" in your original regex matches any whitespace character (space, tab, etc.), not just a regular space.
Avatar of ghostdog74
ghostdog74


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

Open in new window

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.
Avatar of mhotto

ASKER

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

Avatar of mhotto

ASKER

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
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

Avatar of mhotto

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Carl Bohman
Carl Bohman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mhotto

ASKER

Thanks, Bounsy.  Appreciate your taking time to explain it.

Mike
Avatar of mhotto

ASKER

Bounsy, provided the specific solution I, required, with the same tool I, was using.  Bounsy, also added additional explanation.  Excellent Response.