Skip to content

Commit a516e4a

Browse files
committed
Add comment for negative lookbehind and lookahead
1 parent 924df4a commit a516e4a

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

test/regex_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ def test_phoneNumberRegex(data, expected):
129129
@pytest.mark.parametrize("data, expected", CPP_CSHARP_REGEX_TEST_CASES)
130130
def test_cpp_csharpRegex(data, expected):
131131
# https://stackoverflow.com/questions/79435236/how-to-match-c-c-or-c
132-
# The ?: inside the group (?:\+\+|#) just make the group non capturing. The (?<!S) and (?!\S) are called lookarounds, and assert that either whitespace or the start/end precedes/follows the match
132+
# The ?: inside the group (?:\+\+|#) just make the group non capturing. The (?<!S) and (?!\S) are called lookarounds, and assert that either whitespace or the start/end precedes/follows the match.
133+
# (?<!S) is negative lookbehind
134+
# Meaning: Asserts that the current position in the string is not preceded by a non-whitespace character (\S).
135+
# Effect: This effectively matches locations that are preceded by a whitespace character (\s) or the beginning of the string (BOS), without including the whitespace character itself in the final match.
136+
# (?!\S) is negative lookahead
137+
# Meaning: Asserts that the current position in the string is not followed by a non-whitespace character (\S).
138+
# Effect: This effectively matches locations that are followed by a whitespace character (\s) or the end of the string (EOS), without including the following character in the final match.
133139
# matches to be the entire input string
134140
cpp_csharp_regex = r"^C(?:\+\+|#)?$"
135141
# matches perhaps as part of a larger string, with the matches surrounded by whitespace

0 commit comments

Comments
 (0)