Solved

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

Posted on 2009-07-02
12
998 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
Comment Utility
In Perl regex:
s/(.*) /$1"/

Open in new window

0
 
LVL 13

Expert Comment

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

Open in new window

0
 
LVL 13

Expert Comment

by:Carl Bohman
Comment Utility
Note also that "\s" in your original regex matches any whitespace character (space, tab, etc.), not just a regular space.
0
 
LVL 9

Expert Comment

by:ghostdog74
Comment Utility

$\ = "\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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:mhotto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, Bounsy.  Appreciate your taking time to explain it.

Mike
0
 

Author Closing Comment

by:mhotto
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now