Skip to content

Commit 73d3197

Browse files
committed
number handling
1 parent 53c7790 commit 73d3197

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

tests/verbal_expressions_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,20 @@ def test_should_match_url(self):
136136
self.exp = self.v.start_of_line().then('http').maybe('s').then('://').maybe('www.').word().then('.').word().maybe('/').end_of_line().regex()
137137
self.assertRegexpMatches('https://www.google.com/', self.exp, 'Not a valid email')
138138

139-
def test_should_find_named_groups(self):
139+
def test_should_find_number(self):
140+
self.exp = self.v.start_of_line().number().end_of_line().regex()
141+
self.assertRegexpMatches('123', self.exp, 'Number not found')
142+
143+
def test_word_should_find_named_groups(self):
140144
name = "Linus Torvalds"
141145
self.exp = self.v.start_of_line().word(name='first_name').then(' ').word(name='last_name').end_of_line().regex()
142146
match = self.exp.match(name)
143147
self.assertIsNotNone(match)
144-
self.assertTrue(match.group('first_name') == 'Linus')
145-
self.assertTrue(match.group('last_name') == 'Torvalds')
148+
self.assertEquals(match.group('first_name'), 'Linus')
149+
self.assertEquals(match.group('last_name'), 'Torvalds')
150+
151+
def test_number_should_find_named_groups(self):
152+
self.exp = self.v.start_of_line().number('number').end_of_line().regex()
153+
match = self.exp.match('123')
154+
self.assertIsNotNone(match, self.exp.pattern)
155+
self.assertEquals(match.group('number'), '123')

verbalexpressions/verbal_expressions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def start_of_line(self):
7373
return self.add('^')
7474

7575
@re_escape
76-
def find(self, value, name=None):
77-
return self.add(group(value, name))
76+
def find(self, value):
77+
return self.add(group(value))
7878
then = find
7979

8080
# special characters and groups
@@ -98,6 +98,9 @@ def tab(self):
9898

9999
def word(self, name=None):
100100
return self.add(group(r"\w+", name))
101+
102+
def number(self, name=None):
103+
return self.add(group(r"\d+", name))
101104

102105
def OR(self, value=None):
103106
''' `or` is a python keyword so we use `OR` instead. '''

0 commit comments

Comments
 (0)