How to validate GST ( Goods and Services Tax ) number using Regular Expression | Java program to validate the GST ( Goods and Services Tax ) number using regular expression . ; Function to validate GST ( Goods and Services Tax ) number . ; Regex to check valid GST ( Goods and Services Tax ) number ; Compile the ReGex ; If the string is empty return false ; Pattern class contains matcher ( ) method to find the matching between the given string and the regular expression . ; Return if the string matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import java . util . regex . * ; class GFG { public static boolean isValidGSTNo ( String str ) { String regex = " ^ [ 0-9 ] { 2 } [ A - Z ] { 5 } [ 0-9 ] { 4 } " + " [ A - Z ] { 1 } [ 1-9A - Z ] { 1 } " + " Z [ 0-9A - Z ] { 1 } $ " ; Pattern p = Pattern . compile ( regex ) ; if ( str == null ) { return false ; } Matcher m = p . matcher ( str ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String str1 = "06BZAHM6385P6Z2" ; System . out . println ( isValidGSTNo ( str1 ) ) ; String str2 = "06BZAF67" ; System . out . println ( isValidGSTNo ( str2 ) ) ; String str3 = " AZBZAHM6385P6Z2" ; System . out . println ( isValidGSTNo ( str3 ) ) ; String str4 = "06BZ63AHM85P6Z2" ; System . out . println ( isValidGSTNo ( str4 ) ) ; String str5 = "06BZAHM6385P6F2" ; System . out . println ( isValidGSTNo ( str5 ) ) ; } }