LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2008, 03:01 AM   #1
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Rep: Reputation: 15
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 in C++.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.
 
Old 08-23-2008, 05:55 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You might want to look up regular expressions. The following pattern may help

\[\w]+@[\w]+\.[a-zA-Z]{2,4}\

The above pattern (untested) says:
[\w] any word character
+ one or more times
@ followed by an ampersand
[\w] any word character
+ one or more times
\. followed by a dot
[a-zA-Z] any letter (lower case or upper case)
{2,4} 2 3 or 4 times
 
Old 08-23-2008, 07:04 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
When graemef says regular expressions, he means specifically here Perl Compatible Regular Expressions. In C and C++ these can be used from the pcre library.

I'm not sure the expression he gave covers all legal email addresses, but it does cover quite a lot.

There is a perl module called Mail::CheckUser, which you should be able to dig though to find out how they do it. I dare say that is quite a robust method.

There is also an RFC which describes how to to properly validate an email address using pcre's with PHP. You can also have a look at the source for that and see how it should be done. In either case, the only gotcha is that Perl and PHP hove different quoting / escaping rules from literal strings in C/C++, so you'll need some additional/different \ escapes in your regular expressions.

In any case, I highly recommend you do some tutorials on regular expressions in generally, and pcre's in particular - I consider them an essential tool in any competent programmers toolbox. Goodness knows they've saved me hundred of hours writing painful string parsing code myself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please augment the search function of the LQ-Wiki with wildcards JZL240I-U LQ Suggestions & Feedback 12 07-18-2008 09:26 AM
How to read string in 1 file and search in another file? ameyapandit Programming 7 07-11-2008 12:05 PM
I would like to search a file for a string kevles01@yahoo.co.uk Solaris / OpenSolaris 2 03-28-2008 12:02 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
Grep String Search, and identify source file. carl0ski Linux - General 4 01-21-2006 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration