content_image/842654.jpg
Now, consider complicating the problem a bit; what if you want to match filenames where the extension is not$_{bat}$? Some incorrect attempts: 

.*[.][^b].*$ The first attempt above tries to exclude$_{bat}$by requiring that the first character of the extension is not a b . This is wrong, because the pattern also doesn’t match foo.bar . 

.*[.]([^b]..|.[^a].|..[^t])$ 

The expression gets messier when you try to patch up the first solution by requiring one of the following cases to match: the first character of the extension isn’t b ; the second character isn’t a ; or the third character isn’t t . This accepts foo.bar and rejects autoexec.bat , but it requires a three-letter extension and won’t accept a filename with a two-letter extension such as$_{sendmail.cf}$ . We’ll complicate the pattern again in an effort to fix it. 

.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$ 

In the third attempt, the second and third letters are all made optional in order to allow matching extensions shorter than three characters, such as$_{sendmail.cf}$ . 

The pattern’s getting really complicated now, which makes it hard to read and understand. Worse, if the problem changes and you want to exclude both$_{bat}$and$_{exe}$ as extensions, the pattern would get even more complicated and confusing. 

A negative lookahead cuts through all this confusion: 

.*[.](?!bat$)[^.]*$ The negative lookahead means: if the expression bat doesn’t match at this point, try the rest of the pattern; if$_{bat$}$ does match, the whole pattern will fail. The trailing$_{$}$is required to ensure that something like$_{sample.batch}$ , where the extension only starts with$_{bat}$, will be allowed. The$_{[^.]*}$ makes sure that the pattern works when there are multiple dots in the filename. 

Excluding another filename extension is now easy; simply add it as an alternative inside the assertion. The following pattern excludes filenames that end in either$_{bat}$or$_{exe}$: 

.*[.](?!bat$|exe$)[^.]*$ 

## 5 Modifying Strings 

Up to this point, we’ve simply performed searches against a static string. Regular expressions are also commonly used to modify strings in various ways, using the following pattern methods: 

<img src='content_image/842654.jpg'>

## 5.1 Splitting Strings 

The$_{split()}$ method of a pattern splits a string apart wherever the RE matches, returning a list of the pieces. It’s similar to the$_{split()}$ method of strings but provides much more generality in the delimiters that you can split by; string$_{split()}$ only supports splitting by whitespace or by a fixed string. As you’d expect, there’s a module-level re.split() function, too. 

. split ( string [, maxsplit=0 ] ) Split string by the matches of the regular expression. If capturing parentheses are used in the RE, then their contents will also be returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits are performed. 

You can limit the number of splits made, by passing a value for maxsplit . When maxsplit is nonzero, at most maxsplit splits will be made, and the remainder of the string is returned as the final element of the list. In the following example, the delimiter is any sequence of non-alphanumeric characters. 

