?
Solved

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

Posted on 2009-07-02
12
Medium Priority
?
1,044 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:mhotto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24765756
In Perl regex:
s/(.*) /$1"/

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24765766
Alternately:
s/ (?=[^ ]*$)/"/

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24765835
Note also that "\s" in your original regex matches any whitespace character (space, tab, etc.), not just a regular space.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:ghostdog74
ID: 24769569

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

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 24786050
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
 

Author Comment

by:mhotto
ID: 24794298
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
 

Author Comment

by:mhotto
ID: 24794617
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
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24795014
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
 

Author Comment

by:mhotto
ID: 24795984
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
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 1100 total points
ID: 24796245
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
 

Author Comment

by:mhotto
ID: 24796814
Thanks, Bounsy.  Appreciate your taking time to explain it.

Mike
0
 

Author Closing Comment

by:mhotto
ID: 31599281
Bounsy, provided the specific solution I, required, with the same tool I, was using.  Bounsy, also added additional explanation.  Excellent Response.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question