Solved

search and replace

Posted on 1998-08-20
9
145 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 100 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
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.…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

13 Experts available now in Live!

Get 1:1 Help Now