Hi Michael,<br><br><div class="gmail_quote">On 16 April 2012 01:20, Michael Lewis <span dir="ltr">&lt;<a href="mailto:mjolewis@gmail.com">mjolewis@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">So that you can find the section of a long string that<br>
first matches your regex.<br></div><br></blockquote><div><br></div><div>Why not use re.findall() for that? It seems like re.findall() can fill this need and I wouldn&#39;t need to use re.search()? Can you compare an example circumstance where one would be better suited than the other?</div>
</div></div></blockquote><br>
</div>To add to what Alan&#39;s said:  Regular expressions is a textual specification language that is often used in the specification of tokens, e.g. when designing (for example) some type of computer language, or perhaps a preprocessor for a computer language, the tokens themselves will typically be specified using regular expressions (or something similar), while the grammar of the language will typically be expressed using something like BNF or more typically EBNF (which stands for Extended Backus-Naur Form, after the creators of the syntax.)  <br>
<br>Anyway, so as an example then of where you would not use re.findall() (or indeed re.search()), in terms of compilers  you typically have a scanner and parser component where the scanner has the job of taking the input text which can be seen as a sequence of text characters and and converting it into a sequence of tokens, and this tokenization process may well involve the use of regular expressions, where it only makes sense to match only at the beginning of the text being tokenized/scanned.  So in such a context you&#39;d definitely not even want to use re.search() or re.findall() but rather would probably use re.match() since you&#39;re expressly trying to recognize the next &quot;word&quot; (token) from the text being scanned (and ultimately, parsed.)<br>
<br>Another example: Suppose you&#39;re implementing a preprocessor for a programming language, and you therefore want to ignore most of the actual programming language text (since you&#39;re really only interested in the pre-processor language that interspersed with the &quot;normal&quot; programming language.)  In such a scenario the first (or next) pre-processor language token will likely not be at the beginning of your current program text block, so in such a case re.match() or re.findall() would not be helpful since you want to then find the first token in the text, which may or may not be at the beginning of the string. In such a case you&#39;d therefore like to use re.search().  <br>
<br>HTH,<br><br>Walter<br><br><br>