How to search for a string in a text file using wildcards(*,?)

Hey,
I am trying to write a program that reads a textfile and displays all valid e-mail address(tj@yahoo.com or tn.hall@auburn.edu) that are in the textfile.At this time, I am reading the file line by line, but don't have a clue how to search for a valid e-mail. However, I can search for a particular pharse such as "a" and the program will tell me if "a" is in the file. Overall, I am unsure how to go by searching for an e-mail bcause I don't know the exact format that I am looking for but I know something like this would work *@.com. With this in mind how to search a file using wildcards in C++. Any help will be appreciated.
Hi, that should be eazy. Let's asume that you have the algorithm figured out and focus on wildcards. There is a method called getline. It's second abstraction is istream& getline (char* s, streamsize n, char delim ); and as you may notice the char delim is what you're interested in. You can read more bout it here http://www.cplusplus.com/reference/iostream/istream/getline.html

If you ahve any questions regarding using this function or the algorithm itself, ask away

Gregor
I think what you are looking for is regular expressions. I haven't used them in C++ before, but I think they are not in the standard library, but they are in the Boost library which is a very popular C++ library. Basically regular expressions allow you to search strings for a specific pattern that you define, or make sure the entire string matches that pattern.

I would suggest installing the boost library and google regular expressions using boost, and regular expressions for email addresses.
Thanks!! However,can you give an example of this, I have read the link and am not following. When do you state that you are looking for *@.com(which will be something like tnj@yahoo.com). For example, say a file has the following:
Tiffanie Hall th@auburn.edu tt@adfgjui.dog
Kime Jones knhj@yahoo.com

When reading each line I strip out the valid e-mail address only(in this case:th@auburn.edu, knhj@yahoo.com)!!
I think the search should be looking for *@*.com, *@*.edu, *@*.mil bassically address that are valid.
Below is code for just searching for a particular string("a") that was discussed in original post.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {

char* search = "a"; // search pattern
int offset;
string line;
ifstream Myfile;
Myfile.open ("example.txt");

if(Myfile.is_open())
{
while(!Myfile.eof())
{
getline(Myfile,line);
if ((offset = line.find(search, 0)) != string::npos) {
cout << "found '" << search << endl;
}
}
Myfile.close();
}
else
cout<<"Unable to open this file."<<endl;

return 0;
}


Thanks in advance!!

mahlerfive,
Thanks!! However, I have a question: Is this the only way to do this(simple),this program will be checked on a linux machine and I don't think my professor will take the time to download needed libraries or is this library included in linux.
I don't think your professor will accept a regexp solution. You are supposed to figure out how to recognize an email address yourself.

Take out a piece of paper (or open a text editor) and write yourself a little diagram that shows exactly what characters may or must appear in an address. Consider the beginning of the line and end of the line also when considering how to recognize the ends of the address.

As you work through that, you'll get a better idea of how to write the code to recognize an address.

Remember, once you've read the entire line, you can look at the characters in any order, just like humans do. I suggest you start by looking for the '@' character.

Hope this helps.
in addition to duoas´s post, as far as i read it in an phpo-book^^, the plausibility-check for an email contains:

1. In front of the "@(at)" there is at least 1 or more chars but no "@8(at)".
2. After the "@(at)" there got to be at least 3 or more chars but no "@(at)".
3. Then followes a dot and at least two chars but no dot, not "@(at)" and no numbers.(but as we know an email could be "blabla@laber.co.uk", cant it?[ im not 100% sure about]^^)...

it would be ^[^@]+@[^@]{3,}\.[^\.@0-9]{2,}$ in regular expressions...
Last edited on
Ah, I did not realize it was for an assignment - in that case the prof probably does not want you to use regex unless you have learned about it in class.
Duoas,
Thanks!! I have it working somewhat!!!
Topic archived. No new replies allowed.