How to match a string's end using a regex pattern in Java?

后端 未结 4 1723
Happy的楠姐
Happy的楠姐 2021-02-19 23:56

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

相关标签:
4条回答
  • 2021-02-20 00:22

    Use $:

    Pattern p = Pattern.compile("s$");
    
    0 讨论(0)
  • 2021-02-20 00:37

    take a look for following example:

    String ss = "Developers".replaceAll(".$", " ");
    
    0 讨论(0)
  • 2021-02-20 00:44

    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)
  • 2021-02-20 00:47
        public static void main(String[] args) 
    {
        String message = "hi this message is a test message";
        message = message.replaceAll("message$", "email");
        System.out.println(message);
    }
    

    Check this, http://docs.oracle.com/javase/tutorial/essential/regex/bounds.html

    0 讨论(0)
提交回复
热议问题