Skip to content

Commit b175fc8

Browse files
committed
Merge pull request #6 from ds604/master
Added examples, based on Ruby and JavaScript
2 parents 881a921 + c802c96 commit b175fc8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,54 @@ pip install VerbalExpressions
1010
from verbalexpressions import VerEx
1111
verbal_expression = VerEx()
1212
```
13+
## Examples
14+
15+
### Testing if we have a valid URL
16+
```python
17+
# Create an example of how to test for correctly formed URLs
18+
verbal_expression = VerEx()
19+
tester = verbal_expression. \
20+
start_of_line(). \
21+
find('http'). \
22+
maybe('s'). \
23+
find('://'). \
24+
maybe('www.'). \
25+
anything_but(' '). \
26+
end_of_line()
27+
28+
# Create an example URL
29+
test_url = "https://www.google.com"
30+
31+
# Test if the URL is valid
32+
if tester.match(test_url):
33+
print "Valid URL"
34+
35+
#Print the generated regex
36+
print tester.source() # => ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$
37+
```
38+
### Replacing strings
39+
```python
40+
#Create a test string
41+
replace_me = "Replace bird with a duck"
42+
43+
#Create an expression that looks for the word "bird"
44+
expression = VerEx().find('bird')
45+
46+
#Execute the expression in VerEx
47+
result_VerEx = expression.replace(replace_me, 'duck')
48+
print result_VerEx
49+
50+
#Or we can compile and use the regular expression using re
51+
import re
52+
regexp = expression.compile()
53+
result_re = regexp.sub('duck', replace_me)
54+
print result_re
55+
```
56+
### Shorthand for string replace
57+
```python
58+
result = VerEx().find('red').replace('We have a red house', 'blue')
59+
print result
60+
```
1361

1462
## Developer setup : running the tests
1563
```bash

0 commit comments

Comments
 (0)