mhotto
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
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
Alternately:
s/ (?=[^ ]*$)/"/
Note also that "\s" in your original regex matches any whitespace character (space, tab, etc.), not just a regular space.
$\ = "\n";
while (<>) {
chomp; # strip record separator
@f = split(/\s+/, $_);
$f[-1] = " \"" . $f[-1];
print join($,,@f);
}
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.
ASKER
To all: For learning/instructional/tes ting 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
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
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
"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.
(?=[^ ]*$)
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Bounsy. Appreciate your taking time to explain it.
Mike
Mike
ASKER
Bounsy, provided the specific solution I, required, with the same tool I, was using. Bounsy, also added additional explanation. Excellent Response.
Open in new window