元字符“ $”匹配特定字符串的末尾,即它匹配字符串的最后一个字符。例如,
表达式“ \\ d $ ”与以数字结尾的字符串/行匹配。
表达式“ [az] $ ”匹配以小写字母结尾的字符串/行。
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main(String args[]) {
      //从用户读取字符串
      System.out.println("Enter a String");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = ".*[^a-zA-Z0-9//s]$";
      //编译正则表达式
      Pattern pattern = Pattern.compile(regex);
      //检索匹配器对象
      Matcher matcher = pattern.matcher(input);
      if(matcher.matches()) {
         System.out.println("Match occurred");
      } else {
         System.out.println("Match not occurred");
      }
   }
}Enter a String this is sample text# Match occurred
Enter a String hello how are you Match not occurred
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "\\.$";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 5 input strings: ");
      String input[] = new String[5];
      for (int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //创建一个Pattern对象
      Pattern p = Pattern.compile(regex);
      for(int i=0; i<5;i++) {
         //创建一个Matcher对象
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println("String "+i+" ends with '.'");
         }
      }
   }
}输出结果
Enter 5 input strings: hello how are you. where do you live what is your name. welcome to nhooo The Biggest Online Tutorials Library. String 0 ends with '.' String 2 ends with '.' String 4 ends with '.'