-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
57 lines (44 loc) · 1.28 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from mini_regex.regex import MiniRegex
# from mini_regex.util import nfa_to_table
# Supported Special Chars:
# Repeaters: ?, +, *
# Metachar: .
# Union: |
# RegexClasses (with ranges and negation): [^0-10abc]
# Escaping special chars with double slash: \\
# Also supported:
# - Greedy toggle on regex class (currently only works for find_all_matches())
# Example1: Simple Pattern
pattern = '.el*o'
regex = MiniRegex(pattern)
search_str = 'Hello World!'
result = regex.find_all_matches(search_str)
print(result) # [MatchObj: Hello (0, 4)]
# Example 2: Find All Gmail accounts in a string
regex2 = MiniRegex("[a-zA-Z0-9.]+@gmail\\.com")
search_str = '''
Name: John Doe
Age: 21
email: [email protected]
interests: coding
Name: JaneDoe
Age: 18
email: [email protected]
interests: none
Name: Guido Van Rossum
Age: 63
email: [email protected]
interests: Python!!!
'''
result = regex2.find_all_matches(search_str)
print(result)
# [MatchObj: [email protected] (31, 47),
# MatchObj: [email protected] (174, 199)]
# Example 3: Finding the first gmail in a string, and interacting with a match
# object
match = regex2.first_match(search_str)
if match.has_value():
print("Result span:", match.get_span())
print("Result value: ", match.get_value())
# (31, 47)