Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

search and replace

Posted on 1998-08-20
9
Medium Priority
?
186 Views
Last Modified: 2010-03-05
hello,
I would like to search a string for a pattern,but only delete the last occurrence of the pattern. For example:
  when string is:
      object1.a oject2.b object1
   pattern is
         object1
 after processing, string should become
     object1.a object2.b
however,when string is
   object1.a object2.b object1.c
the pattern is object1
  after processing
  the string still is:
  object1.a object2.b object1.c    
0
Comment
Question by:rongz
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 1204427
$string =~ s/(object1)(.*)$1/$1$2/;
0
 
LVL 5

Expert Comment

by:ecw
ID: 1204428
Whoops missed the last bit of the question off

$string =~ s/(object1)(\.a.*)$1(?!\.c)/$1$2/;
0
 
LVL 5

Accepted Solution

by:
b2pi earned 300 total points
ID: 1204429
ecw, what happens if it's in there more than twice?

For general purposes, try:

$pattern = 'object1'; ## or what have you
$string =~ s/(.*\s)$a(\s.*?|$)/$1$2/;

Note that this will NOT remove the pattern if the only occurence of the pattern is the first thing on the line. It will, however, remove the pattern even if the pattern is not duplicated.
If you don't want that to happen, use

$string =~ s/($a.*\s)$a(\s.*?|$)/$1$2/;


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

 

Author Comment

by:rongz
ID: 1204430
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 

Author Comment

by:rongz
ID: 1204431
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 

Author Comment

by:rongz
ID: 1204432
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204433
<brain cramp>
Use $pattern where it says $a

potential problems with "$string=~s/([.\s]*)($pattern$)/$1;" ?
1.) It won't compile (no trailing /)
2.) it will ONLY work if pattern is at the very end of the string (that also means that
    the line "object1.a object1.b object1 " where $pattern is object1 will leave you with
               "object1.a object1.b object1 "
                                                        ^ see the blasted space? This kind of problem will bite you forever)
3.) if pattern is "object" it will take "object notanobject" and make it into "object notan"

0
 
LVL 85

Expert Comment

by:ozo
ID: 1204434
s/\b\Q$pattern\E(\s*)$/$1/;
0
 

Author Comment

by:rongz
ID: 1204435
thanks all
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

971 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