
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to match end of a particular string/line using Java RegEx
The meta character “$” matches the end of a particular string i.e. it matches the last character of the string. For example,
The expression “\d$” matches the string/line ending with a digit.
The expression “[a-z]$” matches the string/line ending with a lower case alphabet.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = ".*[^a-zA-Z0-9//s]$"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); if(matcher.matches()) { System.out.println("Match occurred"); } else { System.out.println("Match not occurred"); } } }
Output 1
Enter a String this is sample text# Match occurred
Output 2
Enter a String hello how are you Match not occurred
Example 2
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(); } //Creating a Pattern object Pattern p = Pattern.compile(regex); for(int i=0; i<5;i++) { //Creating a Matcher object Matcher m = p.matcher(input[i]); if(m.find()) { System.out.println("String "+i+" ends with '.'"); } } } }
Output
Enter 5 input strings: hello how are you. where do you live what is your name. welcome to tutorialspoint The Biggest Online Tutorials Library. String 0 ends with '.' String 2 ends with '.' String 4 ends with '.'
- Related Articles
- How to match beginning of a particular string/line using Java RegEx
- How to match end of the input using Java RegEx?
- How match a string irrespective of case using Java regex.
- How to match a character from given string including case using Java regex?
- How to match a range of characters using Java regex
- How to match a line not containing a word in Java Regex
- How to match a fixed set of characters using Java RegEx
- How to match any character using Java RegEx
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?
- How to match a particular word in a string using Pattern class in Java?
- How to match a non-word character using Java RegEx?
- How to match a white space equivalent using Java RegEx?
- How to match digits using Java Regular Expression (RegEx)
- How to match non-word boundaries using Java RegEx?

Advertisements