Solved

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

Posted on 2009-07-02
12
1,023 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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

733 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