Given a file as input.txt containing some email ids which are mixed with other data. The task is to read this input file line by line and if any email id is found in that line, write that email id to another file, which is output.txt.
Approach: To detect the email id in that file, a simple solution is Regular expression. First, we have to form a regular expression for email id. Whenever any string in the input.txt file matches with that regular expression which we form for email id, then that matched string will be written to output.txt file.
A string is said to be email id if that string follow below criteria:
- The first character can be lowercase or uppercase alphabet or it can contain any digit from 0 to 9.For this criteria, regular expression [a-zA-Z0-9]
- The rest character after the first character and until reaching @, the characters can be lowercase or uppercase alphabet or it can contain any digit from 0 to 9 or special symbol ‘_’ and ‘.’ .For this criteria, regular expression [a-zA-Z0-9_.]*
- After the above two criteria, the string contain symbol ‘@’.After that string should contain any lowercase or uppercase alphabet or it can contain any digit from 0 to 9.For this criteria, regular expression @[a-zA-Z0-9]
- After containing ‘@’ symbol, the string should contain ‘.’ symbol and after that the string should contain any lowercase or uppercase alphabet.For this criteria, regular expression [.][a-zA-Z]
Below is the implementation of the above approach:
Hello my name is Bishal Dubey and my email id is email@example.com . Welcome to Geeksgorgeeks, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes . My brother, Bikash Dubey having email id firstname.lastname@example.org and my friend Tanu Jain having email id email@example.com contributing to geeksforgeeks. If you like GeeksforGeeks and would like to contribute, you can also mail your article to firstname.lastname@example.org.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java program to read all mobile numbers present in given file
- Count of words that are present in all the given sentences
- Program to print characters present at prime indexes in a given string
- How to generate and read QR code with Java using ZXing Library
- Calculate sum of all numbers present in a string
- Check if the given string of words can be formed from words present in the dictionary
- Python | Check if a Substring is Present in a Given String
- First string from the given array whose reverse is also present in the same array
- Check if given words are present in a string
- Check if binary representations of 0 to N are present as substrings in given binary string
- C program to find and replace a word in a File by another given word
- Java Program for efficiently print all prime factors of a given number
- Java Program to print all permutations of a given string
- Remove characters from the first string which are present in the second string
- Count words present in a string
- Find the character in first string that is present at minimum index in second string
- Find repeated character present first in a string
- Find the largest Alphabetic character present in the string
- Number of substrings of one string present in other
- Most frequent word in first String which is not present in second String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.