java 正则表达式后缀,如何在java中使用正则表达式匹配字符串结尾

I want a regular expression pattern that will match with the end of a string.

I'm implementing a stemming algorithm that will remove suffixes of a word.

e.g. for a word 'Developers' it should match 's'.

i can do it using following code :

Pattern p = Pattern.compile("s");

Matcher m = p.matcher("Developers");

m.relaceAll(" "); // it will replace all 's' with ' '

i want a regular expression that will match only a string's end.

basically i want a method replaceLast().

解决方案

You need to match "s", but only if it is the last character in a word. This is achieved with the boundary assertion $:

input.replaceAll("s$", " ");

If you enhance the regular expression, you can replace multiple suffixes with one call to replaceAll:

input.replaceAll("(ed|s)$", " ");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值