Regex – match start and end of line

In this article, we will take a look at matching start and end of line and word with regular expressions.

In regex, we have following two characters for this

^ To match the start of a line, and
$ To match the end of line

These are called anchors, which do not have any length. Rather, they match a position: start or end.

Regex start of line
To check if a word or line starts with some character or pattern, ^(carat) is used.
This means that the test string should start with a character, regex should start with ^ and the character to test followed by other characters to test. Example,

^s[a-z] will match only those two characters in a word that start with s.
First character of match will be s and the character after s.

Note that if you want to check if a line or word starts with some character, then ^ should be the first character of regex.

It will not work, if placed between the regular expression. Examples are

Expression Examples Description
^s Matches first character of soil, sand, shell It will match first character of a line if it starts with ‘s’.
^she Matches 3 characters of she, shell, sheep Will match 3 characters of a line or word that begins with ‘s’ followed by has ‘h’ and ‘e’.
^s[a-z]* Matches complete words soil, sand, shell etc. Will match words that start with ‘s’ followed by all alphabets.
For a multi word line, this will match the first word if the line starts with ‘s’.
^s[a-z ]* Matches words soil, soil is fertile etc. Will match lines that start with ‘s’.
^\d[a-z]* Matches complete words 6balls, 2boys, 3girls etc. Will match words that start with a number followed by all alphabets.
For a multi word line, this will match the first word if the line starts with a number.
^\d[a-z ]* Matches words 6, 6 balls is one over etc. Will match lines that start with a number.
^[bc][a-z]* Matches ball and call. Will match


Java – Regex start of line

If you are working in java and want to match a string that starts with some pattern programatically using regex, then you can use compile() and match() methods of Pattern class as shown below

// regex to match start of word with number
Pattern pattern = Pattern.compile("^\\d[a-z]*");
Matcher matcher = pattern.matcher("1over");
boolean matches = matcher.matches(); //true
Regex end of line
To test if a word or line ends with a character or pattern, $(dollar) anchor is used in regex.
$ is applied at the end of regex and it will match those words or lines that end with the pattern just before $ symbol. Example,
[a-z]1$ will match the last two characters of a word that ends with 1.

$ will not work, if placed between the regular expression. Examples are

Expression Examples Description
$1 Matches last character of ball1, sand23, shell12 It will match last character of a word if it ends with 1.
tc[h$] Matches the last 3 characters of match, catch, batch Will match last 3 characters of a line or word that ends with ‘tch’.
[a-z]*w$ Matches complete words how, cow, eyebrow etc. Will match words that end with ‘w’.
For a multi word line, this will match the last word if the line ends with ‘w’.
[a-z]*\d$ Matches complete words run12, boys3, girls4 etc. Will match words that end with a number.
For a multi word line, this will match the last word if the line ends with a number.
[a-z ]*\d$ Matches words 6, balls is one over are 6 etc. Will match lines that end with a number.


Java – Regex end of line

With java, you can use compile() and match() methods of Pattern class to match end of line with $ as shown below

// regex to match end of word with number
Pattern pattern = Pattern.compile("[a-z]*\\d$"); 
Matcher matcher = pattern.matcher("over6"); 
boolean matches = matcher.matches(); //true

Hope you understood how to match start and end of line with regex anchors.
Play around with different combinations to clarify further.