Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numbers and named groups #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
number handling
  • Loading branch information
andrii1812 committed Jan 2, 2016
commit 73d31976c70d0d0bcda5cf4c482b742cdca993e6
16 changes: 13 additions & 3 deletions tests/verbal_expressions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,20 @@ def test_should_match_url(self):
self.exp = self.v.start_of_line().then('http').maybe('s').then('://').maybe('www.').word().then('.').word().maybe('/').end_of_line().regex()
self.assertRegexpMatches('https://www.google.com/', self.exp, 'Not a valid email')

def test_should_find_named_groups(self):
def test_should_find_number(self):
self.exp = self.v.start_of_line().number().end_of_line().regex()
self.assertRegexpMatches('123', self.exp, 'Number not found')

def test_word_should_find_named_groups(self):
name = "Linus Torvalds"
self.exp = self.v.start_of_line().word(name='first_name').then(' ').word(name='last_name').end_of_line().regex()
match = self.exp.match(name)
self.assertIsNotNone(match)
self.assertTrue(match.group('first_name') == 'Linus')
self.assertTrue(match.group('last_name') == 'Torvalds')
self.assertEquals(match.group('first_name'), 'Linus')
self.assertEquals(match.group('last_name'), 'Torvalds')

def test_number_should_find_named_groups(self):
self.exp = self.v.start_of_line().number('number').end_of_line().regex()
match = self.exp.match('123')
self.assertIsNotNone(match, self.exp.pattern)
self.assertEquals(match.group('number'), '123')
7 changes: 5 additions & 2 deletions verbalexpressions/verbal_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def start_of_line(self):
return self.add('^')

@re_escape
def find(self, value, name=None):
return self.add(group(value, name))
def find(self, value):
return self.add(group(value))
then = find

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

def word(self, name=None):
return self.add(group(r"\w+", name))

def number(self, name=None):
return self.add(group(r"\d+", name))

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