Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

search and replace

Posted on 1998-08-20
9
Medium Priority
?
184 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
[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
  • 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
Independent Software Vendors: 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 84

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

722 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