Perl substitute from right to left

I have a string:

test1.domain.com.test1.domain.com

I want to replace the string domain.com in the right side for result:

test1.domain.com.test1

When using Perl string replace pattern

myString = "test1.domain.com.test1.domain.com";
replacedString = "domain.com";
resultString = null;
Perl5Util perl=new Perl5Util();
resultString =perl.substitute("s/." + replacedString + "//o",myString );

However, Perl replaces the string "domain.com" from left to right with the result

test1.test1.domain.com

Is there any way to change the behaviour?

Answers


Match the end of the string with $.

perl.substitute("s/." + replacedString + "$//o",myString );

You should probably escape that . as well, since . matches any character. You might need two backslashes (\\.) otherwise the java string will treat it as a special character. If you don't want to escape the . you use the character selection syntax [.].

perl.substitute("s/\\." + replacedString + "$//o",myString );

Need Your Help

How to make @each observer trigger just once

ember.js

I have an each observer which observes an array property. I have an action that goes through the array and sets that property, so it gets fired multiple times. I am wondering if I could make it tri...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.