-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from cs50/regex_module
Move regex to module
- Loading branch information
Showing
4 changed files
with
46 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import re | ||
|
||
|
||
def decimal(number): | ||
""" | ||
Create a regular expression to match the number exactly: | ||
In case of a positive number:: | ||
(?<![\d\-])number(?!(\.?\d)) | ||
In case of a negative number:: | ||
number(?!(\.?\d)) | ||
:code:`(?<![\d\-])` = negative lookbehind, \ | ||
asserts that there are no digits and no - in front of the number. | ||
:code:`(?!(\.?\d))` = negative lookahead, \ | ||
asserts that there are no digits and no additional . followed by digits after the number. | ||
:param number: the number to match in the regex | ||
:type number: any numbers.Number (such as int, float, ...) | ||
:rtype: str | ||
Example usage:: | ||
# Check that 7.0000 is printed with 5 significant figures | ||
check50.run("./prog").stdout(check50.regex.decimal("7.0000")) | ||
""" | ||
negative_lookbehind = fr"(?<![\d\-])" if number >= 0 else "" | ||
return fr"{negative_lookbehind}{re.escape(str(number))}(?!(\.?\d))" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters