HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java Regular Expressions / Regex – Match Start or End of String (Line Anchors)

Regex – Match Start or End of String (Line Anchors)

1. Line Anchors

In regex, anchors are not used to match characters. Rather they match a position i.e. before, after, or between characters. To match start and end of line, we use following anchors:

  • Caret (^) matches the position before the first character in the string.
  • Dollar ($) matches the position right after the last character in the string.

2. Regex patterns to match start of line

DescriptionMatching Pattern
Line starts with number“^\\d” or “^[0-9]”
Line starts with character“^[a-z]” or “^[A-Z]”
Line starts with character (case-insensitive)^[a-zA-Z]
Line starts with word“^word”
Line starts with special character“^[!@#\\$%\\^\\&*\\)\\(+=._-]”
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) 
	{
		System.out.println(Pattern.compile("^[0-9]").matcher("1stKnight").find());

		System.out.println(Pattern.compile("^[a-zA-Z]").matcher("FirstKnight").find());

		System.out.println(Pattern.compile("^First").matcher("FirstKnight").find());

		System.out.println(Pattern.compile("^[!@#\\$%\\^\\&*\\)\\(+=._-]")
										.matcher("*1stKnight").find());
	}
}

Program output.

true
true
true
true

3. Regex patterns to match end of line

DescriptionMatching Pattern
Line ends with number“\\d$” or “[0-9]$”
Line ends with character“[a-z]$” or “[A-Z]$”
Line ends with character (case-insensitive)[a-zA-Z]$
Line ends with word“word$”
Line ends with special character“[!@#\\$%\\^\\&*\\)\\(+=._-]$”
public class Main {
	public static void main(String[] args) 
	{
		System.out.println(Pattern.compile("[0-9]$").matcher("FirstKnight123").find());

		System.out.println(Pattern.compile("[a-zA-Z]$").matcher("FirstKnight").find());

		System.out.println(Pattern.compile("Knight$").matcher("FirstKnight").find());

		System.out.println(Pattern.compile("[!@#\\$%\\^\\&*\\)\\(+=._-]$")
										.matcher("FirstKnight&").find());
	}
}

Program output.

true
true
true
true

Drop me your questions related to programs for regex starts with and ends with java.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces