Working of general symbols in regex

In this post, we will see the working of general Symbols in regex mentioned in this post with a few examples. So, let’s start one by one

1. \^ symbol in regex

This will tell you about the start of your string.
Suppose we have a string “codekru” and we have a regex “^abc“, then regex will not match the string as it starts with cod rather than abc. Instead, if we take a string, say “abcdef“, then it will match our regex. Below is the example code for the same.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Codekru {

	public static void main(String[] args) {
		String regex = "^abc";
		String input1 = "abcdef";
		String input2 = "codekru";
		String input3 = "babcdef";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(input1);
		if (matcher.find()) {
			System.out.println(input1 + " string starts with abc");   // this will get printed 
		} else {
			System.out.println(input1 + " string doesn't starts with abc");
		}

		matcher.reset(input2);
		if (matcher.find()) {
			System.out.println(input2 + " string starts with abc");
		} else {
			System.out.println(input2 + " string doesn't starts with abc"); // this will get printed 
		}

		matcher.reset(input3);
		if (matcher.find()) {
			System.out.println(input3 + " string starts with abc");
		} else {
			System.out.println(input3 + " string doesn't starts with abc"); // this will get printed 
		}
	}

}

Output

abcdef string starts with abc
codekru string doesn't starts with abc
babcdef string doesn't starts with abc

2. $ symbol in regex

This symbol will tell you about the end of the string. Nothing should come after we have introduced $ in the regex. Now here, if we will again take our string “codekru” and will try to match it using $ in our regex. Below is the working of the $ with the java code.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Codekru {

	public static void main(String[] args) {
		String regex = "kru$";
		String input1 = "codekru";
		String input2 = "krucode";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(input1);
		if (matcher.find()) {
			System.out.println(input1 + " string ends with abc");   // this will get printed 
		} else {
			System.out.println(input1 + " string doesn't end with abc");
		}

		matcher.reset(input2);
		if (matcher.find()) {
			System.out.println(input2 + " string ends with abc");
		} else {
			System.out.println(input2 + " string doesn't end with abc"); // this will get printed 
		}

		
	}

}

Output

codekru string ends with abc
krucode string doesn't end with abc

3. \b and \B symbols in regex

\b symbol tells you about the word boundary while \B doesn’t. Let’s have our string “codekru“, and try to match it using \b in our regex. Let’s first try using “\bcodekru\b“. What will happen here? Will it match our string “codekru“. So, the answer is Yes!!!, it will match our string “codekru“, but what if we use “\Bcodekru\B“. How, will the regex behave now? Will it match our string “codekru”. So, here the answer is NO!!, it will not match our string, because it is expecting to have some more words at the beginning and at the end of our string. So, let’s try using “HicodekruHi” and match it with the same regex “\Bcodekru\B”. This time it will match our string.
Still, Confused? Don’t worry, we have got you covered with the below code.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Codekru {

	public static void main(String[] args) {
		String regexwithsmallb = "\\bcodekru\\b";
		String regexwithcapitalB = "\\Bcodekru\\B";
		String input1 = "codekru";
		Pattern pattern1 = Pattern.compile(regexwithsmallb);
		Pattern pattern2 = Pattern.compile(regexwithcapitalB);
		Matcher matcher1 = pattern1.matcher(input1);
		Matcher matcher2 = pattern2.matcher(input1);
		if (matcher1.find()) {
			System.out.println(input1 + " matches the regex"); // this will get printed
		} else {
			System.out.println(input1 + " doesn't maches the regex");
		}
		if (matcher2.find()) {
			System.out.println(input1 + " matches the regex"); 
		} else {
			System.out.println(input1 + " doesn't maches the regex");// this will get printed
		}

		String input2 = "HicodekruHi";

		matcher1.reset(input2);
		matcher2.reset(input2);
		if (matcher1.find()) {
			System.out.println(input2 + " matches the regex"); 
		} else {
			System.out.println(input2 + " doesn't maches the regex");// this will get printed
		}
		if (matcher2.find()) {
			System.out.println(input2 + " matches the regex"); // this will get printed
		} else {
			System.out.println(input2 + " doesn't maches the regex");
		}
	}

Output

codekru matches the regex
codekru doesn't maches the regex
HicodekruHi doesn't maches the regex
HicodekruHi matches the regex

We know your hunger for regex isn’t fulfilled yet. So, below are some more symbols and examples

4. /z and /Z symbols in regex

/z (small z ) will tell you about the end of the string and /Z(capital Z) will also tell you about the end of the string except for the allowable final line terminator.
/z(small z) will match only at the end of the string
/Z(capital Z) will also match at the end of the string but if there is /n at the end of the string, it will match before it
So, what is the difference between /z and /Z? It will be more clear with the below example

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Codekru {

	public static void main(String[] args) {
		String regexwithsmallz = "kru\\z";
		String regexwithcapitalZ = "kru\\Z";
		String input1 = "code code codekru\n";
		Pattern pattern1 = Pattern.compile(regexwithsmallz);
		Pattern pattern2 = Pattern.compile(regexwithcapitalZ);

		Matcher matcher1 = pattern1.matcher(input1);
		Matcher matcher2 = pattern2.matcher(input1);

		if (matcher1.find()) {
			System.out.println(regexwithsmallz+" matches the input");  
		} else {
			System.out.println(regexwithsmallz + " doesn't maches the input");// this will get printed
		}
		if (matcher2.find()) {
			System.out.println(regexwithcapitalZ + " matches the input"); // this will get printed
		} else {
			System.out.println(regexwithcapitalZ + " doesn't maches the input");
		}

	}

}

Output

kru\z doesn't maches the input
kru\Z matches the input

Now you must be wondering that what is the difference between $ and /Z, as both seem to be the same. They mostly differ in the multiline mode which we will discuss in another post

4. […] symbol in regex

This [..] symbol means any charcter that is listed inside the square brackets. Below is the example code to provide the understanding of the same

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Codekru {

	public static void main(String[] args) {
		String regex = "c[od]e";
		String input1 = "coe";
		String input2 = "cde";
		String input3 = "cke";
		Pattern pattern = Pattern.compile(regex);
	
		Matcher matcher = pattern.matcher(input1);
		
		if (matcher.find()) {
			System.out.println(input1+" matches the regex");  // this will get printed
		} else {
			System.out.println(input1 + " doesn't maches the regex");
		}

		matcher.reset(input2);
		if (matcher.find()) {
			System.out.println(input2+" matches the regex");  // this will get printed
		} else {
			System.out.println(input2 + " doesn't maches the regex");
		}
		
		matcher.reset(input3);
		if (matcher.find()) {
			System.out.println(input3+" matches the regex");  
		} else {
			System.out.println(input3 + " doesn't maches the regex");// this will get printed
		}
	}

}

Output –

coe matches the regex
cde matches the regex
cke doesn't maches the regex

If you want to have a look at other symbols in regex ( regular expressions ), then you can have a look at this post, and if you want to see an application of the usage of regex, then you take a sneak peek at this article( Apache log file parsing ).

Hope you find it helpful. If you have any doubts, please feel free to write us in comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *