<p>We've got a scenario that requires us to parse lots of e-mail (plain text), each e-mail 'type' is the result of a script being run against various platforms. Some are tab delimited, some are space delimited, some we simply don't know yet.</p>

<p>We'll need to support more 'formats' in the future too.</p>

<p>Do we go for a solution using:</p>

<ul>
<li>Regex</li>
<li>Simply string searching (using string.IndexOf etc)</li>
<li>Lex/ Yacc</li>
<li>Other</li>
</ul>

<p>The overall solution will be developed in C# 2.0 (hopefully 3.5)</p>

[{'answer_id': 56351, 'author': 'Iain Holder', 'author_id': 1122, 'author_profile': 'https://Stackoverflow.com/users/1122', 'pm_score': 3, 'selected': False, 'text': '<p>Regex.</p>\n\n<p>Regex can solve almost everything except for world peace.  Well maybe world peace too.</p>\n'}, {'answer_id': 56352, 'author': 'Robert Durgin', 'author_id': 3132, 'author_profile': 'https://Stackoverflow.com/users/3132', 'pm_score': 0, 'selected': False, 'text': '<p>Regex would probably be you bes bet, tried and proven. Plus a regular expression can be compiled.</p>\n'}, {'answer_id': 56354, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 1, 'selected': False, 'text': "<p>You probably should have a pluggable system regardless of which type of string parsing you use. So, this system calls upon the right 'plugin' depending on the type of email to parse it.</p>\n"}, {'answer_id': 56359, 'author': 'prakash', 'author_id': 123, 'author_profile': 'https://Stackoverflow.com/users/123', 'pm_score': -1, 'selected': False, 'text': "<p>With as little information you provided, i would choose Regex.</p>\n\n<p>But what kind of information you want to parse and what you would want to do will change the decision to Lex/Yacc maybe..</p>\n\n<p>But it looks like you've already made your mind up with String search :)</p>\n"}, {'answer_id': 56387, 'author': 'Coincoin', 'author_id': 42, 'author_profile': 'https://Stackoverflow.com/users/42', 'pm_score': 3, 'selected': True, 'text': "<p>The three solutions you stated each cover very different needs.</p>\n\n<p><strong>Manual parsing</strong> (simple text search) is the most flexible and the most adaptable, however, it very quickly becomes a real pain in the ass as the parsing required is more complicated.</p>\n\n<p><strong>Regex</strong> are a middle ground, and probably your best bet here. They are powerful, yet flexible as you can yourself add more logic from the code that call the different regex. The main drawback would be speed here.</p>\n\n<p><strong>Lex/Yacc</strong> is really only adapted to very complicated, predictable syntaxes and lacks a lot of post compile flexibility. You can't easily change parser in mid parsing, well actually you can but it's just too heavy and you'd be better using regex instead.</p>\n\n<p>I know this is a cliché <strong>answer</strong>, it all really comes down to what your exact needs are, but from what you said, I would personally probably go with a bag of regex.</p>\n\n<p>As an <strong>alternative</strong>, as Vaibhav poionted out, if you have several different situations that can arise and that you cna easily detect which one is coming, you could make a plugin system that chooses the right algorithm, and those algorithms could all be very different, one using Lex/Yacc in pointy cases and the other using IndexOf and regex for simpler cases.</p>\n"}, {'answer_id': 56398, 'author': 'Josef', 'author_id': 5581, 'author_profile': 'https://Stackoverflow.com/users/5581', 'pm_score': 0, 'selected': False, 'text': '<p>Your best bet is RegEx because it provides a much greater degree of flexibility than any of the other options. </p>\n\n<p>While you could use <code>IndexOf</code> to handle somethings, you may quickly find yourself writing code that looks like:</p>\n\n<blockquote>\n  <p><code>if(s.IndexOf("search1")&gt;-1 || s.IndexOf("search2")&gt;-1 ||...</code></p>\n</blockquote>\n\n<p>That can be handled in one RegEx statement. Plus, there are a lot of place like <a href="http://regexlib.com" rel="nofollow noreferrer">RegExLib.com</a> where you can find folks who have shared regular expressions to solve problems.</p>\n'}, {'answer_id': 56416, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>You must architect your solution to be updatable, so that you can handle unknown situations when they crop up.  Create an interface for parsers that contains not only methods for parsing the emails and returning results in a standard format, but also for examining the email to determine if the parser will execute.</p>\n\n<p>Within your configuration, identify the type of parser you wish to use, set its configuration options, and the configuration for the identifiers which determine if a parser will act or not.  Name the parsers by assembly qualified name so that the types can be instantiated at runtime even if there aren't static links to their assemblies.</p>\n\n<p>Identifiers can implement an interface as well, so you can create different types that check for different things.  For instance, you might create a regex identifier, which parses the email for a specific pattern.  Make sure to make as much information available to the identifier, so that it can make decisions on things like from addresses as well as the content of the email.</p>\n\n<p>When your known parsers can't handle a job, create a new DLL with types that implement the parser and identifier interfaces that can handle the job and drop them in your bin directory.</p>\n"}, {'answer_id': 56431, 'author': 'spoulson', 'author_id': 3347, 'author_profile': 'https://Stackoverflow.com/users/3347', 'pm_score': 1, 'selected': False, 'text': '<p>It depends on what you\'re parsing.  For anything beyond what Regex can handle, I\'ve been using <a href="http://antlr.org" rel="nofollow noreferrer">ANTLR</a>.  Before you jump into recursive descent parsing for the first time, I would research how they work, before attempting to use a framework like this one.  If you subscribe to MSDN Magazine, check the Feb 2008 issue where they have an article on writing one from scratch.</p>\n\n<p>Once you get the understanding, learning ANTLR will be a ton easier.  There are other frameworks out there, but ANTLR seems to have the most community support and public documentation.  The author has also published <a href="https://rads.stackoverflow.com/amzn/click/com/0978739256" rel="nofollow noreferrer" rel="nofollow noreferrer">The Definitive ANTLR Reference: Building Domain-Specific Languages</a>.</p>\n'}, {'answer_id': 56455, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 0, 'selected': False, 'text': '<p>@Coincoin has covered the bases; I just want to add that with regex it\'s particularly easy to end up with hard-to-read, hard-to-maintain code.  Regex is a powerful and very compact language, so that\'s how it often goes.</p>\n\n<p>Using whitespace and comments within the regex can go a long way to make it easier to maintain regexes.  Eric Gunnerson turned me on to this idea.  Here\'s <a href="http://blogs.msdn.com/ericgu/archive/2006/01/27/518554.aspx" rel="nofollow noreferrer">an example</a>.</p>\n'}, {'answer_id': 828270, 'author': 'Geek', 'author_id': 102040, 'author_profile': 'https://Stackoverflow.com/users/102040', 'pm_score': 0, 'selected': False, 'text': '<p>Use PCRE. All other answers are just 2nd Best.</p>\n'}]
['https://Stackoverflow.com/questions/56342', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5791/']