Natural Language Understanding with Python by Deborah A. Dahl

Natural Language Understanding with Python by Deborah A. Dahl

Author:Deborah A. Dahl
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2023-06-27T00:00:00+00:00


General tips for using regular expressions

Regular expressions can easily become very complex and difficult to modify and debug. They can also easily fail to recognize some examples of what they’re supposed to recognize and falsely recognize what they’re not supposed to recognize. While it is tempting to try to match the regular expression so that it recognizes exactly what it is supposed to recognize and nothing else, this can make the expression so complicated that it is difficult to understand. Sometimes, it can be better to miss a few edge cases to keep the expression simple.

If we find that an existing regular expression is failing to find some expressions that we want to capture, or incorrectly finding expressions that we don’t want to capture, it can sometimes be difficult to revise the existing expression without breaking some things that used to work. Here are a few tips that can make regular expressions easier to work with:

Write down what you want the regular expression to match first (such as any two consecutive uppercase alphabetic characters). This will be helpful in both clarifying what you’re trying to do as well as in helping catch any cases that you might have overlooked.

Break complex expressions into components and test each component independently before putting them together. Besides helping with debugging, the component expressions can potentially be reused in other complex expressions. We saw this in the first code block in the previous section with components such as street_name_re.

Use existing tested regular expressions for common expressions, for example, the Python datetime package (see https://docs.python.org/3/library/datetime.html), before trying to write your own regular expressions. They have been well tested over many years by many developers.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.