STRling 2.1.0__tar.gz → 2.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- strling-2.3.0/PKG-INFO +113 -0
- strling-2.3.0/README.md +108 -0
- strling-2.3.0/STRling/__init__.py +1 -0
- strling-2.3.0/STRling/simply/constructors.py +421 -0
- strling-2.3.0/STRling/simply/lookarounds.py +133 -0
- strling-2.3.0/STRling/simply/pattern.py +173 -0
- strling-2.3.0/STRling/simply/sets.py +330 -0
- {strling-2.1.0 → strling-2.3.0}/STRling/simply/static.py +134 -21
- strling-2.3.0/STRling.egg-info/PKG-INFO +113 -0
- {strling-2.1.0 → strling-2.3.0}/STRling.egg-info/SOURCES.txt +1 -4
- {strling-2.1.0 → strling-2.3.0}/setup.py +2 -2
- strling-2.1.0/PKG-INFO +0 -281
- strling-2.1.0/README.md +0 -276
- strling-2.1.0/STRling/__init__.py +0 -1
- strling-2.1.0/STRling/simply/constructors.py +0 -211
- strling-2.1.0/STRling/simply/lookarounds.py +0 -105
- strling-2.1.0/STRling/simply/pattern.py +0 -271
- strling-2.1.0/STRling/simply/sets.py +0 -265
- strling-2.1.0/STRling/templates/__init__.py +0 -1
- strling-2.1.0/STRling/templates/templates.py +0 -0
- strling-2.1.0/STRling/templates_v1.py +0 -77
- strling-2.1.0/STRling.egg-info/PKG-INFO +0 -281
- {strling-2.1.0 → strling-2.3.0}/STRling/simply/__init__.py +0 -0
- {strling-2.1.0 → strling-2.3.0}/STRling.egg-info/dependency_links.txt +0 -0
- {strling-2.1.0 → strling-2.3.0}/STRling.egg-info/top_level.txt +0 -0
- {strling-2.1.0 → strling-2.3.0}/setup.cfg +0 -0
strling-2.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: STRling
|
|
3
|
+
Version: 2.3.0
|
|
4
|
+
Description-Content-Type: text/markdown
|
|
5
|
+
|
|
6
|
+
# STRling
|
|
7
|
+
|
|
8
|
+
## 🗺️ Project Overview
|
|
9
|
+
|
|
10
|
+
STRling is a next-generation production-grade syntax designed as a user interface for writing powerful regular expressions (RegEx) with an object-oriented approach and instructional error handling. STRling recognizes the cryptic nature of raw regular expression can be challenging and susceptibility to errors, which is why STRling looks and feels nothing like RegEx while maintaining the same power and flexibility. String validation should be simple and enjoyable, not a hassle. Best of all, STRling syntax is built upon the RegEx engine, making it fully compatible with all libraries that accept RegEx along with the traditional RegEx methods built-in to Python.
|
|
11
|
+
|
|
12
|
+
## 🗝️ Key Features
|
|
13
|
+
|
|
14
|
+
1. **Beginner Friendly:** You need ZERO knowledge of RegEx jargon to create powerful and clear to read patterns.
|
|
15
|
+
2. **Reliable Logic:** STRling exclusively utilizes built-in python libraries, making it a reliable package.
|
|
16
|
+
3. **Instructional Error Handling:** Errors inevitably occur by drafting invalid patterns, so STRling explains to the developer exactly what was done wrong and how to correct it.
|
|
17
|
+
4. **Consistent and Readable:** Make your projects clear and consistent by using STRling rather than the individual library string validators that each have their own syntax (Sqlite3, Sequelize, Postgres, WTForms... etc.).
|
|
18
|
+
|
|
19
|
+
These key features collectively emphasis and fortify the claim that STRling truly is a next-generation production-grade syntax for pattern handling.
|
|
20
|
+
|
|
21
|
+
## 🎯 Project Mission
|
|
22
|
+
|
|
23
|
+
Our mission is to make RegEx a distant and outdated syntax by abstracting the complexities of RegEx into a clean, readable, and powerful interface. We strive to simplify string operations and ensure that developers can focus on building their applications without the steep learning curve of traditional RegEx. The previous alternative to RegEx was to learn the string validation syntax specific to each user input library, but STRling aims to unify the disparate syntaxes found across various libraries and frameworks into one simple library using only built-in Python packages.
|
|
24
|
+
|
|
25
|
+
## 💾 Installation
|
|
26
|
+
|
|
27
|
+
Install STRling via pip:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
pip install STRling
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## ✨ STRling in action!
|
|
34
|
+
|
|
35
|
+
### 📑 [STRling Documentation on GitHub](https://github.com/TheCyberLocal/STRling/docs/strling_docs.md)
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from STRling import simply as s
|
|
39
|
+
import re
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# Let's make a phone number pattern for the formats below:
|
|
43
|
+
# - (123) 456-7890
|
|
44
|
+
# - 123-456-7890
|
|
45
|
+
# - 123 456 7890
|
|
46
|
+
# - 1234567890
|
|
47
|
+
|
|
48
|
+
# Separator: either space or hyphen
|
|
49
|
+
separator = s.in_chars(' -')
|
|
50
|
+
|
|
51
|
+
# Optional area code part: 123 even if in parenthesis like (123)
|
|
52
|
+
area_code = s.merge( # notice we use merge since we don't want to name the group with parenthesis
|
|
53
|
+
s.may('('), # Optional opening parenthesis
|
|
54
|
+
s.group('area_code', s.digit(3)), # Exactly 3 digits and named for later reference
|
|
55
|
+
s.may(')') # Optional closing parenthesis
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# Central part: 456
|
|
59
|
+
central_part = s.group('central_part', s.digit(3)) # Exactly 3 digits and named for later reference
|
|
60
|
+
|
|
61
|
+
# Last part: 7890
|
|
62
|
+
last_part = s.group("last_part", s.digit(4)) # Exactly 4 digits and named for later reference
|
|
63
|
+
|
|
64
|
+
# Combine all parts into the final phone number pattern
|
|
65
|
+
# Notice we don't name the whole pattern since we can already reference it
|
|
66
|
+
phone_number_pattern = s.merge(
|
|
67
|
+
area_code, # Area code part
|
|
68
|
+
s.may(separator), # Optional separator after area code
|
|
69
|
+
central_part, # Central 3 digits
|
|
70
|
+
s.may(separator), # Optional separator after area code
|
|
71
|
+
last_part # Last part with hyphen and 4 digits
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Example usage
|
|
75
|
+
# Note: To make a pattern a RegEx string compatible with other engines use `str(pattern)`.
|
|
76
|
+
example_text = "(123) 456-7890 and 123-456-7890"
|
|
77
|
+
pattern = re.compile(str(phone_number_pattern)) # Notice str(pattern)
|
|
78
|
+
matches = pattern.finditer(example_text)
|
|
79
|
+
|
|
80
|
+
for match in matches:
|
|
81
|
+
print("Full Match:", match.group())
|
|
82
|
+
print("Area Code:", match.group("area_code"))
|
|
83
|
+
print("Central Part:", match.group("central_part"))
|
|
84
|
+
print("Last Part:", match.group("last_part"))
|
|
85
|
+
print()
|
|
86
|
+
|
|
87
|
+
# Output:
|
|
88
|
+
# Full Match: (123) 456-7890
|
|
89
|
+
# Area Code: 123
|
|
90
|
+
# Central Part: 456
|
|
91
|
+
# Last Part: 7890
|
|
92
|
+
|
|
93
|
+
# Full Match: 123-456-7890
|
|
94
|
+
# Area Code: 123
|
|
95
|
+
# Central Part: 456
|
|
96
|
+
# Last Part: 7890
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Simplify your string validation and matching tasks with STRling, the all-in-one solution for developers who need a powerful yet user-friendly tool for working with strings. No longer write RegEx using complex jargon or the various syntaxes string validation specific to independent libraries. Download and start using STRling today!
|
|
100
|
+
|
|
101
|
+
## 🌎 Locations
|
|
102
|
+
[](https://pypi.org/project/STRling/)
|
|
103
|
+
[](https://github.com/TheCyberLocal/STRling)
|
|
104
|
+
|
|
105
|
+
## 🌐 Socials
|
|
106
|
+
[](https://linkedin.com/in/tzm01)
|
|
107
|
+
|
|
108
|
+
## 💖 Support
|
|
109
|
+
If you find my content helpful or interesting, consider buying me a coffee. Every cup is greatly appreciated and fuels my work!
|
|
110
|
+
|
|
111
|
+
[](https://buymeacoffee.com/thecyberlocal)
|
|
112
|
+
[](https://www.paypal.com/paypalme/TheCyberLocal)
|
|
113
|
+
[](https://www.venmo.com/TheCyberLocal)
|
strling-2.3.0/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# STRling
|
|
2
|
+
|
|
3
|
+
## 🗺️ Project Overview
|
|
4
|
+
|
|
5
|
+
STRling is a next-generation production-grade syntax designed as a user interface for writing powerful regular expressions (RegEx) with an object-oriented approach and instructional error handling. STRling recognizes the cryptic nature of raw regular expression can be challenging and susceptibility to errors, which is why STRling looks and feels nothing like RegEx while maintaining the same power and flexibility. String validation should be simple and enjoyable, not a hassle. Best of all, STRling syntax is built upon the RegEx engine, making it fully compatible with all libraries that accept RegEx along with the traditional RegEx methods built-in to Python.
|
|
6
|
+
|
|
7
|
+
## 🗝️ Key Features
|
|
8
|
+
|
|
9
|
+
1. **Beginner Friendly:** You need ZERO knowledge of RegEx jargon to create powerful and clear to read patterns.
|
|
10
|
+
2. **Reliable Logic:** STRling exclusively utilizes built-in python libraries, making it a reliable package.
|
|
11
|
+
3. **Instructional Error Handling:** Errors inevitably occur by drafting invalid patterns, so STRling explains to the developer exactly what was done wrong and how to correct it.
|
|
12
|
+
4. **Consistent and Readable:** Make your projects clear and consistent by using STRling rather than the individual library string validators that each have their own syntax (Sqlite3, Sequelize, Postgres, WTForms... etc.).
|
|
13
|
+
|
|
14
|
+
These key features collectively emphasis and fortify the claim that STRling truly is a next-generation production-grade syntax for pattern handling.
|
|
15
|
+
|
|
16
|
+
## 🎯 Project Mission
|
|
17
|
+
|
|
18
|
+
Our mission is to make RegEx a distant and outdated syntax by abstracting the complexities of RegEx into a clean, readable, and powerful interface. We strive to simplify string operations and ensure that developers can focus on building their applications without the steep learning curve of traditional RegEx. The previous alternative to RegEx was to learn the string validation syntax specific to each user input library, but STRling aims to unify the disparate syntaxes found across various libraries and frameworks into one simple library using only built-in Python packages.
|
|
19
|
+
|
|
20
|
+
## 💾 Installation
|
|
21
|
+
|
|
22
|
+
Install STRling via pip:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
pip install STRling
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## ✨ STRling in action!
|
|
29
|
+
|
|
30
|
+
### 📑 [STRling Documentation](./docs/strling_docs.md)
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from STRling import simply as s
|
|
34
|
+
import re
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Let's make a phone number pattern for the formats below:
|
|
38
|
+
# - (123) 456-7890
|
|
39
|
+
# - 123-456-7890
|
|
40
|
+
# - 123 456 7890
|
|
41
|
+
# - 1234567890
|
|
42
|
+
|
|
43
|
+
# Separator: either space or hyphen
|
|
44
|
+
separator = s.in_chars(' -')
|
|
45
|
+
|
|
46
|
+
# Optional area code part: 123 even if in parenthesis like (123)
|
|
47
|
+
area_code = s.merge( # notice we use merge since we don't want to name the group with parenthesis
|
|
48
|
+
s.may('('), # Optional opening parenthesis
|
|
49
|
+
s.group('area_code', s.digit(3)), # Exactly 3 digits and named for later reference
|
|
50
|
+
s.may(')') # Optional closing parenthesis
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# Central part: 456
|
|
54
|
+
central_part = s.group('central_part', s.digit(3)) # Exactly 3 digits and named for later reference
|
|
55
|
+
|
|
56
|
+
# Last part: 7890
|
|
57
|
+
last_part = s.group("last_part", s.digit(4)) # Exactly 4 digits and named for later reference
|
|
58
|
+
|
|
59
|
+
# Combine all parts into the final phone number pattern
|
|
60
|
+
# Notice we don't name the whole pattern since we can already reference it
|
|
61
|
+
phone_number_pattern = s.merge(
|
|
62
|
+
area_code, # Area code part
|
|
63
|
+
s.may(separator), # Optional separator after area code
|
|
64
|
+
central_part, # Central 3 digits
|
|
65
|
+
s.may(separator), # Optional separator after area code
|
|
66
|
+
last_part # Last part with hyphen and 4 digits
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# Example usage
|
|
70
|
+
# Note: To make a pattern a RegEx string compatible with other engines use `str(pattern)`.
|
|
71
|
+
example_text = "(123) 456-7890 and 123-456-7890"
|
|
72
|
+
pattern = re.compile(str(phone_number_pattern)) # Notice str(pattern)
|
|
73
|
+
matches = pattern.finditer(example_text)
|
|
74
|
+
|
|
75
|
+
for match in matches:
|
|
76
|
+
print("Full Match:", match.group())
|
|
77
|
+
print("Area Code:", match.group("area_code"))
|
|
78
|
+
print("Central Part:", match.group("central_part"))
|
|
79
|
+
print("Last Part:", match.group("last_part"))
|
|
80
|
+
print()
|
|
81
|
+
|
|
82
|
+
# Output:
|
|
83
|
+
# Full Match: (123) 456-7890
|
|
84
|
+
# Area Code: 123
|
|
85
|
+
# Central Part: 456
|
|
86
|
+
# Last Part: 7890
|
|
87
|
+
|
|
88
|
+
# Full Match: 123-456-7890
|
|
89
|
+
# Area Code: 123
|
|
90
|
+
# Central Part: 456
|
|
91
|
+
# Last Part: 7890
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Simplify your string validation and matching tasks with STRling, the all-in-one solution for developers who need a powerful yet user-friendly tool for working with strings. No longer write RegEx using complex jargon or the various syntaxes string validation specific to independent libraries. Download and start using STRling today!
|
|
95
|
+
|
|
96
|
+
## 🌎 Locations
|
|
97
|
+
[](https://pypi.org/project/STRling/)
|
|
98
|
+
[](https://github.com/TheCyberLocal/STRling)
|
|
99
|
+
|
|
100
|
+
## 🌐 Socials
|
|
101
|
+
[](https://linkedin.com/in/tzm01)
|
|
102
|
+
|
|
103
|
+
## 💖 Support
|
|
104
|
+
If you find my content helpful or interesting, consider buying me a coffee. Every cup is greatly appreciated and fuels my work!
|
|
105
|
+
|
|
106
|
+
[](https://buymeacoffee.com/thecyberlocal)
|
|
107
|
+
[](https://www.paypal.com/paypalme/TheCyberLocal)
|
|
108
|
+
[](https://www.venmo.com/TheCyberLocal)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from . import simply
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
|
|
2
|
+
from .pattern import STRlingError, Pattern, lit
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
############################
|
|
7
|
+
# Constructor Methods
|
|
8
|
+
########
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def any_of(*patterns):
|
|
12
|
+
"""
|
|
13
|
+
Matches any provided pattern, including patterns consisting of subpatterns.
|
|
14
|
+
|
|
15
|
+
Example: simply as s
|
|
16
|
+
- Matches 3 digits followed by 3 letters.
|
|
17
|
+
|
|
18
|
+
pattern1 = s.merge(s.digit(3), s.letter(3))
|
|
19
|
+
|
|
20
|
+
- Matches 3 letters followed by 3 digits.
|
|
21
|
+
|
|
22
|
+
pattern2 = s.merge(s.letter(3), s.digit(3))
|
|
23
|
+
|
|
24
|
+
- Matches either pattern1 or pattern2.
|
|
25
|
+
|
|
26
|
+
either_pattern = s.any_of(pattern1, pattern2)
|
|
27
|
+
|
|
28
|
+
Parameters:
|
|
29
|
+
- *patterns (Pattern/str): One or more patterns to be matched.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
- Pattern: A Pattern object representing the OR combination of the given patterns.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
# Check all patterns are instance of Pattern or str
|
|
36
|
+
clean_patterns = []
|
|
37
|
+
for pattern in patterns:
|
|
38
|
+
if isinstance(pattern, str):
|
|
39
|
+
pattern = lit(pattern)
|
|
40
|
+
|
|
41
|
+
if not isinstance(pattern, Pattern):
|
|
42
|
+
message = """
|
|
43
|
+
Method: simply.any_of(*patterns)
|
|
44
|
+
|
|
45
|
+
The parameters must be instances of `Pattern` or `str`.
|
|
46
|
+
|
|
47
|
+
Use a string such as "123abc$" to match literal characters, or use a predefined set like `simply.letter()`.
|
|
48
|
+
"""
|
|
49
|
+
raise STRlingError(message)
|
|
50
|
+
|
|
51
|
+
clean_patterns.append(pattern)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# Count named groups and raise error if not unique
|
|
55
|
+
named_group_counts = {}
|
|
56
|
+
|
|
57
|
+
for pattern in clean_patterns:
|
|
58
|
+
for group_name in pattern.named_groups:
|
|
59
|
+
if group_name in named_group_counts:
|
|
60
|
+
named_group_counts[group_name] += 1
|
|
61
|
+
else:
|
|
62
|
+
named_group_counts[group_name] = 1
|
|
63
|
+
|
|
64
|
+
duplicates = {name: count for name, count in named_group_counts.items() if count > 1}
|
|
65
|
+
if duplicates:
|
|
66
|
+
duplicate_info = ", ".join([f"{name}: {count}" for name, count in duplicates.items()])
|
|
67
|
+
message = f"""
|
|
68
|
+
Method: simply.any_of(*patterns)
|
|
69
|
+
|
|
70
|
+
Named groups must be unique.
|
|
71
|
+
Duplicate named groups found: {duplicate_info}.
|
|
72
|
+
|
|
73
|
+
If you need later reference change the named group argument to `simply.capture()`.
|
|
74
|
+
If you don't need later reference change the named group argument to `simply.merge()`.
|
|
75
|
+
"""
|
|
76
|
+
raise STRlingError(message)
|
|
77
|
+
|
|
78
|
+
sub_names = named_group_counts.keys()
|
|
79
|
+
|
|
80
|
+
joined = '|'.join(str(p) for p in clean_patterns)
|
|
81
|
+
new_pattern = f'(?:{joined})'
|
|
82
|
+
|
|
83
|
+
return Pattern(new_pattern, composite=True, named_groups=sub_names)
|
|
84
|
+
|
|
85
|
+
def may(*patterns):
|
|
86
|
+
"""
|
|
87
|
+
Optionally matches the provided patterns. If this pattern is absent, surrounding patterns can still match.
|
|
88
|
+
|
|
89
|
+
Example: simply as s
|
|
90
|
+
- Matches any letter, along with any trailing digit.
|
|
91
|
+
|
|
92
|
+
pattern = s.merge(s.letter(), s.may(s.digit()))
|
|
93
|
+
|
|
94
|
+
In the text, "AB2" the pattern above matches 'A' and 'B2'.
|
|
95
|
+
|
|
96
|
+
Parameters:
|
|
97
|
+
- *patterns (Pattern/str): One or more patterns to be optionally matched.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
- Pattern: A Pattern object representing the optional match of the given patterns.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
# Check all patterns are instance of Pattern or str
|
|
104
|
+
clean_patterns = []
|
|
105
|
+
for pattern in patterns:
|
|
106
|
+
if isinstance(pattern, str):
|
|
107
|
+
pattern = lit(pattern)
|
|
108
|
+
|
|
109
|
+
if not isinstance(pattern, Pattern):
|
|
110
|
+
message = """
|
|
111
|
+
Method: simply.may(*patterns)
|
|
112
|
+
|
|
113
|
+
The parameters must be instances of `Pattern` or `str`.
|
|
114
|
+
|
|
115
|
+
Use a string such as "123abc$" to match literal characters, or use a predefined set like `simply.letter()`.
|
|
116
|
+
"""
|
|
117
|
+
raise STRlingError(message)
|
|
118
|
+
|
|
119
|
+
clean_patterns.append(pattern)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# Count named groups and raise error if not unique
|
|
123
|
+
named_group_counts = {}
|
|
124
|
+
|
|
125
|
+
for pattern in clean_patterns:
|
|
126
|
+
for group_name in pattern.named_groups:
|
|
127
|
+
if group_name in named_group_counts:
|
|
128
|
+
named_group_counts[group_name] += 1
|
|
129
|
+
else:
|
|
130
|
+
named_group_counts[group_name] = 1
|
|
131
|
+
|
|
132
|
+
duplicates = {name: count for name, count in named_group_counts.items() if count > 1}
|
|
133
|
+
if duplicates:
|
|
134
|
+
duplicate_info = ", ".join([f"{name}: {count}" for name, count in duplicates.items()])
|
|
135
|
+
message = f"""
|
|
136
|
+
Method: simply.may(*patterns)
|
|
137
|
+
|
|
138
|
+
Named groups must be unique.
|
|
139
|
+
Duplicate named groups found: {duplicate_info}.
|
|
140
|
+
|
|
141
|
+
If you need later reference change the named group argument to `simply.capture()`.
|
|
142
|
+
If you don't need later reference change the named group argument to `simply.merge()`.
|
|
143
|
+
"""
|
|
144
|
+
raise STRlingError(message)
|
|
145
|
+
|
|
146
|
+
sub_names = named_group_counts.keys()
|
|
147
|
+
|
|
148
|
+
joined = merge(*clean_patterns)
|
|
149
|
+
new_pattern = f'{joined}?'
|
|
150
|
+
|
|
151
|
+
return Pattern(new_pattern, composite=True, named_groups=sub_names)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def merge(*patterns):
|
|
156
|
+
"""
|
|
157
|
+
Combines the provided patterns into one larger pattern.
|
|
158
|
+
|
|
159
|
+
Example: simply as s
|
|
160
|
+
- Matches any digit, comma, or period.
|
|
161
|
+
|
|
162
|
+
merged_pattern = s.merge(s.digit(), ',.')
|
|
163
|
+
|
|
164
|
+
Parameters:
|
|
165
|
+
- *patterns (Pattern/str): One or more patterns to be concatenated.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
- Pattern: A Pattern object representing the concatenation of the given patterns.
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
# Check all patterns are instance of Pattern or str
|
|
172
|
+
clean_patterns = []
|
|
173
|
+
for pattern in patterns:
|
|
174
|
+
if isinstance(pattern, str):
|
|
175
|
+
pattern = lit(pattern)
|
|
176
|
+
|
|
177
|
+
if not isinstance(pattern, Pattern):
|
|
178
|
+
message = """
|
|
179
|
+
Method: simply.merge(*patterns)
|
|
180
|
+
|
|
181
|
+
The parameters must be instances of `Pattern` or `str`.
|
|
182
|
+
|
|
183
|
+
Use a string such as "123abc$" to match literal characters, or use a predefined set like `simply.letter()`.
|
|
184
|
+
"""
|
|
185
|
+
raise STRlingError(message)
|
|
186
|
+
|
|
187
|
+
clean_patterns.append(pattern)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# Count named groups and raise error if not unique
|
|
191
|
+
named_group_counts = {}
|
|
192
|
+
|
|
193
|
+
for pattern in clean_patterns:
|
|
194
|
+
for group_name in pattern.named_groups:
|
|
195
|
+
if group_name in named_group_counts:
|
|
196
|
+
named_group_counts[group_name] += 1
|
|
197
|
+
else:
|
|
198
|
+
named_group_counts[group_name] = 1
|
|
199
|
+
|
|
200
|
+
duplicates = {name: count for name, count in named_group_counts.items() if count > 1}
|
|
201
|
+
if duplicates:
|
|
202
|
+
duplicate_info = ", ".join([f"{name}: {count}" for name, count in duplicates.items()])
|
|
203
|
+
message = f"""
|
|
204
|
+
Method: simply.merge(*patterns)
|
|
205
|
+
|
|
206
|
+
Named groups must be unique.
|
|
207
|
+
Duplicate named groups found: {duplicate_info}.
|
|
208
|
+
|
|
209
|
+
If you need later reference change the named group argument to `simply.capture()`.
|
|
210
|
+
If you don't need later reference change the named group argument to `simply.merge()`.
|
|
211
|
+
"""
|
|
212
|
+
raise STRlingError(message)
|
|
213
|
+
|
|
214
|
+
sub_names = named_group_counts.keys()
|
|
215
|
+
|
|
216
|
+
joined = ''.join(str(p) for p in clean_patterns)
|
|
217
|
+
new_pattern = f'(?:{joined})'
|
|
218
|
+
|
|
219
|
+
return Pattern(new_pattern, composite=True, named_groups=sub_names)
|
|
220
|
+
|
|
221
|
+
def capture(*patterns):
|
|
222
|
+
"""
|
|
223
|
+
Creates a numbered group that can be indexed for extracting this part of the match.
|
|
224
|
+
|
|
225
|
+
- Captures CANNOT be invoked with a range.
|
|
226
|
+
|
|
227
|
+
s.capture(s.digit(), s.letter())(1, 2) <== INVALID
|
|
228
|
+
|
|
229
|
+
- Captures CAN be invoked with a number of copies.
|
|
230
|
+
|
|
231
|
+
s.capture(s.digit(), s.letter())(3) <== VALID
|
|
232
|
+
|
|
233
|
+
Example: simply as s
|
|
234
|
+
- Matches any digit, comma, or period.
|
|
235
|
+
|
|
236
|
+
captured_pattern = s.capture(s.digit(), ',.')
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
Parameters:
|
|
240
|
+
- *patterns (Pattern/str): One or more patterns to be captured.
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
- Pattern: A Pattern object representing the capturing group of the given patterns.
|
|
244
|
+
|
|
245
|
+
Referencing: simply as s
|
|
246
|
+
three_digit_group = s.capture(s.digit(3))
|
|
247
|
+
|
|
248
|
+
four_groups_of_three = three_digit_groups(4)
|
|
249
|
+
|
|
250
|
+
example_text = "Here is a number: 111222333444"
|
|
251
|
+
|
|
252
|
+
match = re.search(str(four_groups_of_three), example_text) # Notice str(pattern)
|
|
253
|
+
|
|
254
|
+
print("Full Match:", match.group())
|
|
255
|
+
print("First:", match.group(1))
|
|
256
|
+
print("Second:", match.group(2))
|
|
257
|
+
print("Third:", match.group(3))
|
|
258
|
+
print("Fourth:", match.group(4))
|
|
259
|
+
|
|
260
|
+
# Output:
|
|
261
|
+
# Full Match: 111222333444
|
|
262
|
+
# First: 111
|
|
263
|
+
# Second: 222
|
|
264
|
+
# Third: 333
|
|
265
|
+
# Fourth: 444
|
|
266
|
+
"""
|
|
267
|
+
|
|
268
|
+
# Check all patterns are instance of Pattern or str
|
|
269
|
+
clean_patterns = []
|
|
270
|
+
for pattern in patterns:
|
|
271
|
+
if isinstance(pattern, str):
|
|
272
|
+
pattern = lit(pattern)
|
|
273
|
+
|
|
274
|
+
if not isinstance(pattern, Pattern):
|
|
275
|
+
message = """
|
|
276
|
+
Method: simply.capture(*patterns)
|
|
277
|
+
|
|
278
|
+
The parameters must be instances of `Pattern` or `str`.
|
|
279
|
+
|
|
280
|
+
Use a string such as "123abc$" to match literal characters, or use a predefined set like `simply.letter()`.
|
|
281
|
+
"""
|
|
282
|
+
raise STRlingError(message)
|
|
283
|
+
|
|
284
|
+
clean_patterns.append(pattern)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
# Count named groups and raise error if not unique
|
|
288
|
+
named_group_counts = {}
|
|
289
|
+
|
|
290
|
+
for pattern in clean_patterns:
|
|
291
|
+
for group_name in pattern.named_groups:
|
|
292
|
+
if group_name in named_group_counts:
|
|
293
|
+
named_group_counts[group_name] += 1
|
|
294
|
+
else:
|
|
295
|
+
named_group_counts[group_name] = 1
|
|
296
|
+
|
|
297
|
+
duplicates = {name: count for name, count in named_group_counts.items() if count > 1}
|
|
298
|
+
if duplicates:
|
|
299
|
+
duplicate_info = ", ".join([f"{name}: {count}" for name, count in duplicates.items()])
|
|
300
|
+
message = f"""
|
|
301
|
+
Method: simply.capture(*patterns)
|
|
302
|
+
|
|
303
|
+
Named groups must be unique.
|
|
304
|
+
Duplicate named groups found: {duplicate_info}.
|
|
305
|
+
|
|
306
|
+
If you need later reference change the named group argument to `simply.capture()`.
|
|
307
|
+
If you don't need later reference change the named group argument to `simply.merge()`.
|
|
308
|
+
"""
|
|
309
|
+
raise STRlingError(message)
|
|
310
|
+
|
|
311
|
+
sub_names = named_group_counts.keys()
|
|
312
|
+
|
|
313
|
+
joined = ''.join(str(p) for p in clean_patterns)
|
|
314
|
+
new_pattern = f'({joined})'
|
|
315
|
+
|
|
316
|
+
return Pattern(new_pattern, composite=True, numbered_group=True, named_groups=sub_names)
|
|
317
|
+
|
|
318
|
+
def group(name, *patterns):
|
|
319
|
+
"""
|
|
320
|
+
Creates a unique named group that can be referenced for extracting this part of the match.
|
|
321
|
+
|
|
322
|
+
- Groups CANNOT be invoked with a range.
|
|
323
|
+
|
|
324
|
+
s.group('name', s.digit())(1, 2) <== INVALID
|
|
325
|
+
|
|
326
|
+
Example: simply as s
|
|
327
|
+
- Matches any digit followed by a comma and period.
|
|
328
|
+
|
|
329
|
+
named_pattern = s.group('my_group', s.digit(), ',.')
|
|
330
|
+
|
|
331
|
+
Parameters:
|
|
332
|
+
- name (str): The name of the capturing group.
|
|
333
|
+
- *patterns (Pattern/str): One or more patterns to be captured.
|
|
334
|
+
|
|
335
|
+
Returns:
|
|
336
|
+
- Pattern: A Pattern object representing the named capturing group of the given patterns.
|
|
337
|
+
|
|
338
|
+
Referencing: simply as s
|
|
339
|
+
first = s.group("first", s.digit(3))
|
|
340
|
+
|
|
341
|
+
second = s.group("second", s.digit(3))
|
|
342
|
+
|
|
343
|
+
third = s.group("third", s.digit(4))
|
|
344
|
+
|
|
345
|
+
phone_number_pattern = s.merge(first, "-", second, "-", third)
|
|
346
|
+
|
|
347
|
+
example_text = "Here is a phone number: 123-456-7890."
|
|
348
|
+
|
|
349
|
+
match = re.search(str(phone_number_pattern), example_text) # Notice str(pattern)
|
|
350
|
+
|
|
351
|
+
print("Full Match:", match.group())
|
|
352
|
+
print("First:", match.group("first"))
|
|
353
|
+
print("Second:", match.group("second"))
|
|
354
|
+
print("Third:", match.group("third"))
|
|
355
|
+
|
|
356
|
+
# Output:
|
|
357
|
+
# Full Match: 123-456-7890
|
|
358
|
+
# First: 123
|
|
359
|
+
# Second: 456
|
|
360
|
+
# Third: 7890
|
|
361
|
+
"""
|
|
362
|
+
|
|
363
|
+
if not isinstance(name, str):
|
|
364
|
+
message = """
|
|
365
|
+
Method: simply.group(name, *patterns)
|
|
366
|
+
|
|
367
|
+
The group is missing a specified name.
|
|
368
|
+
The `name` parameter must be a string like 'group_name'.
|
|
369
|
+
"""
|
|
370
|
+
raise STRlingError(message)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
# Check all patterns are instance of Pattern or str
|
|
374
|
+
clean_patterns = []
|
|
375
|
+
for pattern in patterns:
|
|
376
|
+
if isinstance(pattern, str):
|
|
377
|
+
pattern = lit(pattern)
|
|
378
|
+
|
|
379
|
+
if not isinstance(pattern, Pattern):
|
|
380
|
+
message = """
|
|
381
|
+
Method: simply.group(name, *patterns)
|
|
382
|
+
|
|
383
|
+
The parameters must be instances of `Pattern` or `str`.
|
|
384
|
+
|
|
385
|
+
Use a string such as "123abc$" to match literal characters, or use a predefined set like `simply.letter()`.
|
|
386
|
+
"""
|
|
387
|
+
raise STRlingError(message)
|
|
388
|
+
|
|
389
|
+
clean_patterns.append(pattern)
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
# Count named groups and raise error if not unique
|
|
393
|
+
named_group_counts = {}
|
|
394
|
+
|
|
395
|
+
for pattern in clean_patterns:
|
|
396
|
+
for group_name in pattern.named_groups:
|
|
397
|
+
if group_name in named_group_counts:
|
|
398
|
+
named_group_counts[group_name] += 1
|
|
399
|
+
else:
|
|
400
|
+
named_group_counts[group_name] = 1
|
|
401
|
+
|
|
402
|
+
duplicates = {name: count for name, count in named_group_counts.items() if count > 1}
|
|
403
|
+
if duplicates:
|
|
404
|
+
duplicate_info = ", ".join([f"{name}: {count}" for name, count in duplicates.items()])
|
|
405
|
+
message = f"""
|
|
406
|
+
Method: simply.group(name, *patterns)
|
|
407
|
+
|
|
408
|
+
Named groups must be unique.
|
|
409
|
+
Duplicate named groups found: {duplicate_info}.
|
|
410
|
+
|
|
411
|
+
If you need later reference change the named group argument to `simply.capture()`.
|
|
412
|
+
If you don't need later reference change the named group argument to `simply.merge()`.
|
|
413
|
+
"""
|
|
414
|
+
raise STRlingError(message)
|
|
415
|
+
|
|
416
|
+
sub_names = named_group_counts.keys()
|
|
417
|
+
|
|
418
|
+
joined = ''.join(str(p) for p in clean_patterns)
|
|
419
|
+
new_pattern = f'(?P<{name}>{joined})'
|
|
420
|
+
|
|
421
|
+
return Pattern(new_pattern, composite=True, named_groups=[name, *sub_names])
|