Skip to content

Commit ad7e688

Browse files
committed
added match whitespaces
1 parent 8ef6f24 commit ad7e688

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

python 2/koans/about_regex.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ def test_anything_but_matching(self):
144144
+ "na2.xls\n" \
145145
+ "sa1.xls\n" \
146146
+ "ca1.xls"
147-
m = re.search("[ns]a[^0-9]\.xls", string)
147+
m = re.search(__, string)
148148
self.assertTrue(m and m.group(0) and m.group(0)== 'sam.xls', "I want to find the name sam")
149149

150+
def using_metacharacters_escaping(self):
151+
"""
152+
Lesson 3 Using metacharacters
153+
154+
Metacharacters are characters that have special meaning within regular expressions.
155+
156+
Metacharacters can be escaped by preceding them with a backslash, therefore \. matches .
157+
"""
158+
string = "var myArray = new Array();\n" \
159+
+ "if (myArray[0]) { \n" \
160+
+ "}"
161+
m = re.search("myArray[0]", string) #TIP: This pattern matches "myArray0" because [ and ] are metacharacters
162+
self.assertTrue(m and m.group(0) and m.group(0)== 'myArray[0]', "I want to find myArray[0]")
163+
164+
def using_metacharacters_matching_white_spaces(self):
165+
"""
166+
Lesson 3 Matching whitespace character
167+
168+
Sometimes you'll have to match nonprinting whitespace characters embedded in your text. For example tab characters
169+
or line breaks .
170+
In this cases you can use these special metacharacters:
171+
[\b] Backspace
172+
\f Form feed
173+
\n Line feed
174+
\r Carriage return
175+
\t Tab
176+
\v Vertical tab
177+
178+
"""
179+
f = open('koans/regex_cvs', 'r')
180+
string = f.read()
181+
#This text contains a series of records in comma-delimited format (cvs). Before processing the records, you need
182+
# to remove any blank lines in the data.
183+
m = re.search("", string)
184+
self.assertTrue(m and m.group(0) and m.group(0)== '\n\n', "I want to find the blank lines")
185+

python 2/koans/regex_solutions.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ test_using_character_set_ranges:
1414
test_using_multiple_ranges:
1515
#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f] OR #[0-9A-Fa-f]{6} but the latter will be seen later
1616
test_anything_but_matching:
17-
[ns]a[^0-9]\.xls"
17+
[ns]a[^0-9]\.xls
18+
using_metacharacters_escaping:
19+
myArray\[0\]
20+
using_metacharacters_macthing_white_spaces:
21+
\n\n
22+
IMPORTANT: Windows uses a carriage return line fedd combination used as an end-of-line marker, so you need to use \r\n.
23+

0 commit comments

Comments
 (0)