java.util.regex.PatternSyntaxException: Illegal character range near index

The java exception java.util.regex.PatternSyntaxException: Illegal character range near index occurs when an Illegal or inappropriate character range is used to match the regular expression. The range is defined by a hyphen in between minimum and maximum values. If an invalid minimum or maximum value is specified in the regular expression, the exception java.util.regex.PatternSyntaxException: Illegal character range near index is thrown.

In regular expression, the character range will be defined using a hyphen character in between minimum and maximum characters. For example, the value of ‘A’ is 65 and the value of ‘C’ is 67. The range is specified as the [A-C] range. The value of the character range is 65 to 67. The example of an invalid range is [C-A]. When an invalid range is specified or an invalid character is entered, the regular expression can not define the range of characters.

The range will be defined using the minimum and maximum values. The ‘-‘ hyphen is used between the minimum and the maximum values. If an invalid range is defined or an invalid character is entered or some special character is used without an escape character, the exception java.util.regex.PatternSyntaxException: Illegal character range near index will be thrown.



Exception

The java exception java.util.regex.PatternSyntaxException: Illegal character range near index will be thrown as a stack trace below.

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 3
[l-c]
   ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.range(Pattern.java:2655)
	at java.util.regex.Pattern.clazz(Pattern.java:2562)
	at java.util.regex.Pattern.sequence(Pattern.java:2063)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2223)
	at com.yawintutor.StringReplaceAll.main(StringReplaceAll.java:9)


How to reproduce this exception

If an invalid range is defined in a regular expression, either an invalid character is entered or a special character is used without an escape character, the range of values can not be determined. Hence, the exception java.util.regex.PatternSyntaxException: Illegal character range near index will be thrown.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Welcome-test";
		String pattern = "[l-c]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 3
[l-c]
   ^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.range(Pattern.java:2655)
	at java.util.regex.Pattern.clazz(Pattern.java:2562)
	at java.util.regex.Pattern.sequence(Pattern.java:2063)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.replaceAll(String.java:2223)
	at com.yawintutor.StringReplaceAll.main(StringReplaceAll.java:9)


Root Cause

The regular expression supports to specify the range of characters in the search string. The starting character and ending character should be specified with ‘-‘ hyphen symbol. If the invalid starting character or ending character is provided in the range, the regular expression can not identify the range of the characters in the search string. Hence the exception will be thrown.



Solution 1

The range in the search string must have valid minimum value and maximum value in the regular expression. The minimum character value must be smaller than the maximum character value. The example below show the valid range value that will identify the range of characters in the search string.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Welcome-test";
		String pattern = "[c-l]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Welcome-test
Output String : Wom-tst


Solution 2

If the regular expression required to match the characters with ‘-‘ hyphen. The hyphen character must be added with escape character. The hyphen is a special character that is used to identify the range of values in the regular expression. The example below will show the characters l, c and ‘-‘ hyphen will be replaced.

package com.yawintutor;

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Welcome-test";
		String pattern = "[l\\-c]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Welcome-test
Output String : Weometest


Solution 3

If the special characters such as !, [ ,] etc are used as range characters, the escape character should be added before the special characters. In the example below, the “\” is used as a range character without escape characters in the regular expression. The value of the letter ‘W’ lies in between the value of the character ‘A’ and ‘\’.

package com.yawintutor;

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

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Welcome-test";
		String pattern = "[A-\\]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Exception

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 4
[A-\]
    ^
	at java.util.regex.Pattern.error(Pattern.java:1955)

Solution

package com.yawintutor;

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

public class StringReplaceAll {
	public static void main(String[] args) {
		String str = "Welcome-test";
		String pattern = "[A-\\\\]";
		String str2;

		str2 = str.replaceAll(pattern, "");

		System.out.println("Given  String : " + str);
		System.out.println("Output String : " + str2);
	}
}

Output

Given  String : Welcome-test
Output String : elcome-test



Related Articles

Leave a Reply

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