By HARISH SS
Java program to validate and extract Aadhaar numbers from text using regex in a file.
main
method, a Scanner
object is created to read the file.while
loop is used to iterate through each word in the file using the Scanner
object.\\d{4}-\\d{4}-\\d{4}
is defined to match Aadhaar numbers in the format of "####-####-####" (four digits, a hyphen, four digits, a hyphen, and four digits).lastIndexOf
method. If a colon is found, the program assumes that the Aadhaar number is present after the colon.substring
starting from the index after the colon.Pattern
object is created using the regular expression pattern.Matcher
object is initialized with the extracted number and the pattern.find
method of the Matcher
object is called to check if the number matches the defined pattern.catch
block handles the FileNotFoundException
in case the specified file is not found.Note: The code uses @SuppressWarnings("resource")
annotation, which suggests that the Scanner
object is not closed explicitly because it goes out of scope when the loop ends. However, it's generally recommended to close resources like Scanner
explicitly using the close
method or by utilizing the try-with-resources statement to ensure proper resource management.
Make sure to provide a valid file path to an existing text file for the program to read and search for Aadhaar numbers.
user1:2222-3333-4444
user2:4444-5555-32
2222-3333-4444 is valid Addhar Number.
4444-5555-32 is not valid Addhar Number.
Submitted by HARISH SS (Harishss2002)
Download packets of source code on Coders Packet
Comments