简体   繁体   English

Perl替代品从右到左

[英]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: 我想在右侧替换字符串domain.com以获得结果:

test1.domain.com.test1

When using Perl string replace pattern 使用Perl字符串替换模式时

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 但是,Perl用结果从左到右替换字符串“ domain.com”

test1.test1.domain.com

Is there any way to change the behaviour? 有什么办法可以改变行为吗?

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. 您可能需要两个反斜杠( \\\\. ),否则Java字符串会将其视为特殊字符。 If you don't want to escape the . 如果您不想逃脱. you use the character selection syntax [.] . 您使用字符选择语法[.]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM