Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

search and replace

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
rongz
Asked:
rongz
  • 4
  • 2
  • 2
  • +1
1 Solution
 
ecwCommented:
$string =~ s/(object1)(.*)$1/$1$2/;
0
 
ecwCommented:
Whoops missed the last bit of the question off

$string =~ s/(object1)(\.a.*)$1(?!\.c)/$1$2/;
0
 
b2piCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rongzAuthor Commented:
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 
rongzAuthor Commented:
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 
rongzAuthor Commented:
I tried the solution. Not work. What's the $a.
Is there any potential problem with following:

$string=~s/([.\s]*)($pattern$)/$1;
0
 
b2piCommented:
<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
 
ozoCommented:
s/\b\Q$pattern\E(\s*)$/$1/;
0
 
rongzAuthor Commented:
thanks all
0

Featured Post

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!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now