Solved

search and replace

Posted on 1998-08-20
9
134 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

24 Experts available now in Live!

Get 1:1 Help Now