STRling 2.0.0__tar.gz → 2.2.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.0.0 → strling-2.2.0}/PKG-INFO +96 -54
- {strling-2.0.0 → strling-2.2.0}/README.md +95 -53
- strling-2.2.0/STRling/__init__.py +2 -0
- strling-2.2.0/STRling/patterns.py +68 -0
- strling-2.2.0/STRling/simply/constructors.py +421 -0
- strling-2.2.0/STRling/simply/lookarounds.py +133 -0
- strling-2.2.0/STRling/simply/pattern.py +173 -0
- strling-2.2.0/STRling/simply/sets.py +330 -0
- {strling-2.0.0 → strling-2.2.0}/STRling/simply/static.py +43 -23
- {strling-2.0.0 → strling-2.2.0}/STRling/templates_v1.py +6 -6
- {strling-2.0.0 → strling-2.2.0}/STRling.egg-info/PKG-INFO +96 -54
- {strling-2.0.0 → strling-2.2.0}/STRling.egg-info/SOURCES.txt +2 -3
- {strling-2.0.0 → strling-2.2.0}/setup.py +1 -1
- strling-2.0.0/STRling/__init__.py +0 -1
- strling-2.0.0/STRling/simply/constructors.py +0 -156
- strling-2.0.0/STRling/simply/lookarounds.py +0 -124
- strling-2.0.0/STRling/simply/pattern.py +0 -160
- strling-2.0.0/STRling/simply/sets.py +0 -225
- strling-2.0.0/STRling/templates/__init__.py +0 -1
- strling-2.0.0/STRling/templates/templates.py +0 -0
- {strling-2.0.0 → strling-2.2.0}/STRling/simply/__init__.py +0 -0
- {strling-2.0.0 → strling-2.2.0}/STRling.egg-info/dependency_links.txt +0 -0
- {strling-2.0.0 → strling-2.2.0}/STRling.egg-info/top_level.txt +0 -0
- {strling-2.0.0 → strling-2.2.0}/setup.cfg +0 -0
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: STRling
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Description-Content-Type: text/markdown
|
|
5
5
|
|
|
6
6
|
# STRling
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## 🗺️ Project Overview
|
|
9
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
|
|
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
11
|
|
|
12
|
-
###
|
|
12
|
+
### 🗝️ Key Features
|
|
13
13
|
|
|
14
|
-
1. Beginner Friendly
|
|
15
|
-
2. Common Templates Built-in
|
|
16
|
-
3. Reliable Logic
|
|
17
|
-
4. Instructional Error Handling
|
|
18
|
-
5. Consistent and Readable
|
|
14
|
+
1. **Beginner Friendly:** You need ZERO knowledge of RegEx jargon to create powerful and clear to read patterns.
|
|
15
|
+
2. **Common Templates Built-in:** STRling has predefined templates for commonly used patterns, so forget about even writing our simple syntax for most cases. (phone number, email, url... etc.)
|
|
16
|
+
3. **Reliable Logic:** STRling exclusively utilizes built-in python libraries, making it a reliable package.
|
|
17
|
+
4. **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.
|
|
18
|
+
5. **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.).
|
|
19
19
|
|
|
20
20
|
These key features collectively emphasis and fortify the claim that STRling truly is a next-generation production-grade syntax for pattern handling.
|
|
21
21
|
|
|
22
|
-
###
|
|
22
|
+
### 🎯 Project Mission
|
|
23
23
|
|
|
24
24
|
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.
|
|
25
25
|
|
|
26
|
-
### Installation
|
|
26
|
+
### 💾 Installation
|
|
27
27
|
|
|
28
28
|
Install STRling via pip:
|
|
29
29
|
|
|
@@ -31,7 +31,7 @@ Install STRling via pip:
|
|
|
31
31
|
pip install STRling
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
## ✨ STRling in action!
|
|
35
35
|
|
|
36
36
|
```python
|
|
37
37
|
from STRling import simply as s
|
|
@@ -45,20 +45,20 @@ import re
|
|
|
45
45
|
# - 1234567890
|
|
46
46
|
|
|
47
47
|
# Separator: either space or hyphen
|
|
48
|
-
separator = s.
|
|
48
|
+
separator = s.in_chars(' -')
|
|
49
49
|
|
|
50
50
|
# Optional area code part: 123 even if in parenthesis like (123)
|
|
51
51
|
area_code = s.merge( # notice we use merge since we don't want to name the group with parenthesis
|
|
52
|
-
s.may(
|
|
52
|
+
s.may('('), # Optional opening parenthesis
|
|
53
53
|
s.group('area_code', s.digit(3)), # Exactly 3 digits and named for later reference
|
|
54
|
-
s.may(
|
|
54
|
+
s.may(')') # Optional closing parenthesis
|
|
55
55
|
)
|
|
56
56
|
|
|
57
57
|
# Central part: 456
|
|
58
|
-
central_part = s.group('central_part', s.digit(3))
|
|
58
|
+
central_part = s.group('central_part', s.digit(3)) # Exactly 3 digits and named for later reference
|
|
59
59
|
|
|
60
60
|
# Last part: 7890
|
|
61
|
-
last_part = s.group("last_part", s.digit(4))
|
|
61
|
+
last_part = s.group("last_part", s.digit(4)) # Exactly 4 digits and named for later reference
|
|
62
62
|
|
|
63
63
|
# Combine all parts into the final phone number pattern
|
|
64
64
|
# Notice we don't name the whole pattern since we can already reference it
|
|
@@ -73,7 +73,7 @@ phone_number_pattern = s.merge(
|
|
|
73
73
|
# Example usage
|
|
74
74
|
# Note: To make a pattern a RegEx string compatible with other engines use `str(pattern)`.
|
|
75
75
|
example_text = "(123) 456-7890 and 123-456-7890"
|
|
76
|
-
pattern = re.compile(str(phone_number_pattern))
|
|
76
|
+
pattern = re.compile(str(phone_number_pattern)) # Notice str(pattern)
|
|
77
77
|
matches = pattern.finditer(example_text)
|
|
78
78
|
|
|
79
79
|
for match in matches:
|
|
@@ -95,7 +95,7 @@ for match in matches:
|
|
|
95
95
|
# Last Part: 7890
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
## 📑 STRling Documentation
|
|
99
99
|
|
|
100
100
|
### Don't worry if you can't remember it all!
|
|
101
101
|
We have well structured and explanatory docustrings for each function
|
|
@@ -113,13 +113,13 @@ import re
|
|
|
113
113
|
# For example, simply.letter(1, 3) will match 1 to 3 letters.
|
|
114
114
|
|
|
115
115
|
# But some methods take an unknown number of parameters and can't distinguish the range.
|
|
116
|
-
# For example, s.merge(simply.letter(), simply.digit(), 1, 2). <====
|
|
116
|
+
# For example, s.merge(simply.letter(), simply.digit(), 1, 2). <==== INVALID
|
|
117
117
|
|
|
118
|
-
# However, all methods allow setting the range by
|
|
118
|
+
# However, all methods allow setting the range by externally invoking unless otherwise specified.
|
|
119
119
|
# For example, simply.letter()(1, 2) is the same as simply.letter(1, 2).
|
|
120
120
|
|
|
121
121
|
# This external invocation may seem useless, but it can solve our earlier issue.
|
|
122
|
-
# For example, s.merge(simply.letter(), simply.digit())(1, 2). <====
|
|
122
|
+
# For example, s.merge(simply.letter(), simply.digit())(1, 2). <==== VALID
|
|
123
123
|
|
|
124
124
|
# Notice for all functions (where repetition is valid) we can invoke the range outside the parameters,
|
|
125
125
|
# but it is primarily useful for functions with an unknown number of parameters.
|
|
@@ -148,6 +148,18 @@ s.tab() # Matches a tab character.
|
|
|
148
148
|
s.carriage() # Matches a carriage return character.
|
|
149
149
|
s.bound() # Matches a boundary character.
|
|
150
150
|
|
|
151
|
+
####################
|
|
152
|
+
# Anchors
|
|
153
|
+
####################
|
|
154
|
+
|
|
155
|
+
s.start() # Matches the start of a line.
|
|
156
|
+
# There is no `simply.not_start()` function.
|
|
157
|
+
# Instead, use `simply.not_behind(simply.start())`.
|
|
158
|
+
|
|
159
|
+
s.end() # Matches the end of a line.
|
|
160
|
+
# There is no `simply.not_end()` function.
|
|
161
|
+
# Instead, use `simply.not_ahead(simply.end())`.
|
|
162
|
+
|
|
151
163
|
####################
|
|
152
164
|
# Custom Sets
|
|
153
165
|
####################
|
|
@@ -160,43 +172,26 @@ s.between(0, 9) # Matches any digit from 0 to 9.
|
|
|
160
172
|
s.between('a', 'z') # Matches any lowercase letter from 'a' to 'z'.
|
|
161
173
|
s.between('A', 'Z') # Matches any uppercase letter from 'A' to 'Z'.
|
|
162
174
|
|
|
163
|
-
# Matches any
|
|
164
|
-
s.
|
|
165
|
-
#
|
|
166
|
-
# Key distinctions:
|
|
167
|
-
# - simply.in_() typically processes faster than simply.any_of().
|
|
168
|
-
# - simply.any_of() can process composite patterns while simply.in_() cannot.
|
|
169
|
-
# A composite pattern is one consisting of smaller combined patterns.
|
|
170
|
-
|
|
171
|
-
####################
|
|
172
|
-
# Anchors
|
|
173
|
-
####################
|
|
174
|
-
|
|
175
|
-
s.start() # Matches the start of a line.
|
|
176
|
-
# There is no `simply.not_start()` function.
|
|
177
|
-
# Instead, use `simply.not_behind(simply.start())`.
|
|
178
|
-
|
|
179
|
-
s.end() # Matches the end of a line.
|
|
180
|
-
# There is no `simply.not_end()` function.
|
|
181
|
-
# Instead, use `simply.not_ahead(simply.end())`.
|
|
175
|
+
# Matches any provided patterns, but they can't include subpatterns.
|
|
176
|
+
s.in_chars(s.letter(), s.digit(), ',.') # Matches any letter, digit, comma, and period.
|
|
177
|
+
# A composite pattern is one consisting of subpatterns (created by constructors and lookarounds).
|
|
182
178
|
|
|
183
179
|
####################
|
|
184
180
|
# Constructors
|
|
185
181
|
####################
|
|
186
182
|
|
|
187
|
-
s.any_of() # Matches any
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
# A composite pattern is one consisting of smaller combined patterns.
|
|
193
|
-
s.any_of(pattern1, pattern2) # Matches only if one of the patterns listed is present.
|
|
183
|
+
s.any_of() # Matches any pattern, including patterns consisting of subpatterns.
|
|
184
|
+
# A composite pattern is one consisting of subpatterns (created by constructors and lookarounds).
|
|
185
|
+
pattern1 = s.merge(s.digit(3), s.letter(3)) # Matches 3 digits followed by 3 letters.
|
|
186
|
+
pattern2 = s.merge(s.letter(3), s.digit(3)) # Matches 3 letters followed by 3 digits.
|
|
187
|
+
s.any_of(pattern1, pattern2) # Matches either pattern1 or pattern2
|
|
194
188
|
|
|
195
189
|
|
|
196
190
|
s.may() # Optionally matches the provided patterns.
|
|
197
191
|
# If a `may` pattern isn't there, it still will match the rest of the patterns.
|
|
198
|
-
# In the text, "ABC2" the patter below matches ['A', 'B', 'C2']
|
|
199
192
|
s.merge(s.letter(), s.may(s.digit()))
|
|
193
|
+
# Matches any letter, and includes any digit following the letter.
|
|
194
|
+
# In the text, "AB2" the pattern above matches 'A' and 'B2'.
|
|
200
195
|
|
|
201
196
|
|
|
202
197
|
s.merge() # Combines multiple patterns into one larger pattern.
|
|
@@ -207,12 +202,53 @@ s.capture() # Creates a numbered group that can be indexed for extracting part
|
|
|
207
202
|
# Capture is used the same as merge.
|
|
208
203
|
s.capture(s.letter(), s.digit())
|
|
209
204
|
|
|
205
|
+
# Captures CANNOT be invoked with a range: s.capture(s.digit(), s.letter())(1, 2) <== INVALID
|
|
206
|
+
# Captures CAN be invoked with a number of copies: s.capture(s.digit(), s.letter())(3) <== VALID
|
|
207
|
+
|
|
208
|
+
three_digit_group = s.capture(s.digit(3))
|
|
209
|
+
four_groups_of_three = three_digit_groups(4)
|
|
210
|
+
|
|
211
|
+
example_text = "Here is a number: 111222333444"
|
|
212
|
+
match = re.search(str(four_groups_of_three), example_text) # Notice str(pattern)
|
|
213
|
+
|
|
214
|
+
print("Full Match:", match.group())
|
|
215
|
+
print("First:", match.group(1))
|
|
216
|
+
print("Second:", match.group(2))
|
|
217
|
+
print("Third:", match.group(3))
|
|
218
|
+
print("Fourth:", match.group(4))
|
|
219
|
+
|
|
220
|
+
# Output:
|
|
221
|
+
# Full Match: 111222333444
|
|
222
|
+
# First: 111
|
|
223
|
+
# Second: 222
|
|
224
|
+
# Third: 333
|
|
225
|
+
# Fourth: 444
|
|
226
|
+
|
|
210
227
|
|
|
211
228
|
s.group() # Creates a named group that can be referenced for extracting part of a match.
|
|
212
229
|
# group is used the same as merge and capture but it takes a string name as the first argument.
|
|
213
230
|
s.group('my_group', s.letter(), s.digit())
|
|
214
|
-
|
|
231
|
+
|
|
232
|
+
# Unlike merge and capture, groups CANNOT be invoked with a range.
|
|
215
233
|
# This is because group names must be unique in a pattern.
|
|
234
|
+
# s.group('unique_name', s.digit())(1, 2) <== INVALID
|
|
235
|
+
|
|
236
|
+
# Groups can easily be referenced from the match by name
|
|
237
|
+
# assuming the numbers have been grouped and named properly.
|
|
238
|
+
example_text = "Here is a phone number: 123-456-7890."
|
|
239
|
+
match = re.search(str(phone_number_pattern), example_text) # Notice str(pattern)
|
|
240
|
+
|
|
241
|
+
print("Full Match:", match.group())
|
|
242
|
+
print("Area Code:", match.group("area_code"))
|
|
243
|
+
print("Central Part:", match.group("central_part"))
|
|
244
|
+
print("Last Part:", match.group("last_part"))
|
|
245
|
+
|
|
246
|
+
# Output:
|
|
247
|
+
# Full Match: 123-456-7890
|
|
248
|
+
# Area Code: 123
|
|
249
|
+
# Central Part: 456
|
|
250
|
+
# Last Part: 7890
|
|
251
|
+
|
|
216
252
|
|
|
217
253
|
####################
|
|
218
254
|
# Lookarounds
|
|
@@ -236,10 +272,16 @@ s.merge(s.behind(s.digit()), s.letter()) # Only matches a letter preceded by a
|
|
|
236
272
|
|
|
237
273
|
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!
|
|
238
274
|
|
|
239
|
-
|
|
275
|
+
## 🌎 Locations
|
|
276
|
+
[](https://pypi.org/project/STRling/)
|
|
277
|
+
[](https://github.com/TheCyberLocal/STRling)
|
|
240
278
|
|
|
241
|
-
|
|
279
|
+
## 🌐 Socials
|
|
280
|
+
[](https://linkedin.com/in/tzm01)
|
|
242
281
|
|
|
243
|
-
|
|
282
|
+
## 💖 Support
|
|
283
|
+
If you find my content helpful or interesting, consider buying me a coffee. Every cup is greatly appreciated and fuels my work!
|
|
244
284
|
|
|
245
|
-
|
|
285
|
+
[](https://buymeacoffee.com/thecyberlocal)
|
|
286
|
+
[](https://www.paypal.com/paypalme/TheCyberLocal)
|
|
287
|
+
[](https://www.venmo.com/TheCyberLocal)
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
# STRling
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 🗺️ Project Overview
|
|
4
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
|
|
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
6
|
|
|
7
|
-
###
|
|
7
|
+
### 🗝️ Key Features
|
|
8
8
|
|
|
9
|
-
1. Beginner Friendly
|
|
10
|
-
2. Common Templates Built-in
|
|
11
|
-
3. Reliable Logic
|
|
12
|
-
4. Instructional Error Handling
|
|
13
|
-
5. Consistent and Readable
|
|
9
|
+
1. **Beginner Friendly:** You need ZERO knowledge of RegEx jargon to create powerful and clear to read patterns.
|
|
10
|
+
2. **Common Templates Built-in:** STRling has predefined templates for commonly used patterns, so forget about even writing our simple syntax for most cases. (phone number, email, url... etc.)
|
|
11
|
+
3. **Reliable Logic:** STRling exclusively utilizes built-in python libraries, making it a reliable package.
|
|
12
|
+
4. **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.
|
|
13
|
+
5. **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.).
|
|
14
14
|
|
|
15
15
|
These key features collectively emphasis and fortify the claim that STRling truly is a next-generation production-grade syntax for pattern handling.
|
|
16
16
|
|
|
17
|
-
###
|
|
17
|
+
### 🎯 Project Mission
|
|
18
18
|
|
|
19
19
|
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.
|
|
20
20
|
|
|
21
|
-
### Installation
|
|
21
|
+
### 💾 Installation
|
|
22
22
|
|
|
23
23
|
Install STRling via pip:
|
|
24
24
|
|
|
@@ -26,7 +26,7 @@ Install STRling via pip:
|
|
|
26
26
|
pip install STRling
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
## ✨ STRling in action!
|
|
30
30
|
|
|
31
31
|
```python
|
|
32
32
|
from STRling import simply as s
|
|
@@ -40,20 +40,20 @@ import re
|
|
|
40
40
|
# - 1234567890
|
|
41
41
|
|
|
42
42
|
# Separator: either space or hyphen
|
|
43
|
-
separator = s.
|
|
43
|
+
separator = s.in_chars(' -')
|
|
44
44
|
|
|
45
45
|
# Optional area code part: 123 even if in parenthesis like (123)
|
|
46
46
|
area_code = s.merge( # notice we use merge since we don't want to name the group with parenthesis
|
|
47
|
-
s.may(
|
|
47
|
+
s.may('('), # Optional opening parenthesis
|
|
48
48
|
s.group('area_code', s.digit(3)), # Exactly 3 digits and named for later reference
|
|
49
|
-
s.may(
|
|
49
|
+
s.may(')') # Optional closing parenthesis
|
|
50
50
|
)
|
|
51
51
|
|
|
52
52
|
# Central part: 456
|
|
53
|
-
central_part = s.group('central_part', s.digit(3))
|
|
53
|
+
central_part = s.group('central_part', s.digit(3)) # Exactly 3 digits and named for later reference
|
|
54
54
|
|
|
55
55
|
# Last part: 7890
|
|
56
|
-
last_part = s.group("last_part", s.digit(4))
|
|
56
|
+
last_part = s.group("last_part", s.digit(4)) # Exactly 4 digits and named for later reference
|
|
57
57
|
|
|
58
58
|
# Combine all parts into the final phone number pattern
|
|
59
59
|
# Notice we don't name the whole pattern since we can already reference it
|
|
@@ -68,7 +68,7 @@ phone_number_pattern = s.merge(
|
|
|
68
68
|
# Example usage
|
|
69
69
|
# Note: To make a pattern a RegEx string compatible with other engines use `str(pattern)`.
|
|
70
70
|
example_text = "(123) 456-7890 and 123-456-7890"
|
|
71
|
-
pattern = re.compile(str(phone_number_pattern))
|
|
71
|
+
pattern = re.compile(str(phone_number_pattern)) # Notice str(pattern)
|
|
72
72
|
matches = pattern.finditer(example_text)
|
|
73
73
|
|
|
74
74
|
for match in matches:
|
|
@@ -90,7 +90,7 @@ for match in matches:
|
|
|
90
90
|
# Last Part: 7890
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
## 📑 STRling Documentation
|
|
94
94
|
|
|
95
95
|
### Don't worry if you can't remember it all!
|
|
96
96
|
We have well structured and explanatory docustrings for each function
|
|
@@ -108,13 +108,13 @@ import re
|
|
|
108
108
|
# For example, simply.letter(1, 3) will match 1 to 3 letters.
|
|
109
109
|
|
|
110
110
|
# But some methods take an unknown number of parameters and can't distinguish the range.
|
|
111
|
-
# For example, s.merge(simply.letter(), simply.digit(), 1, 2). <====
|
|
111
|
+
# For example, s.merge(simply.letter(), simply.digit(), 1, 2). <==== INVALID
|
|
112
112
|
|
|
113
|
-
# However, all methods allow setting the range by
|
|
113
|
+
# However, all methods allow setting the range by externally invoking unless otherwise specified.
|
|
114
114
|
# For example, simply.letter()(1, 2) is the same as simply.letter(1, 2).
|
|
115
115
|
|
|
116
116
|
# This external invocation may seem useless, but it can solve our earlier issue.
|
|
117
|
-
# For example, s.merge(simply.letter(), simply.digit())(1, 2). <====
|
|
117
|
+
# For example, s.merge(simply.letter(), simply.digit())(1, 2). <==== VALID
|
|
118
118
|
|
|
119
119
|
# Notice for all functions (where repetition is valid) we can invoke the range outside the parameters,
|
|
120
120
|
# but it is primarily useful for functions with an unknown number of parameters.
|
|
@@ -143,6 +143,18 @@ s.tab() # Matches a tab character.
|
|
|
143
143
|
s.carriage() # Matches a carriage return character.
|
|
144
144
|
s.bound() # Matches a boundary character.
|
|
145
145
|
|
|
146
|
+
####################
|
|
147
|
+
# Anchors
|
|
148
|
+
####################
|
|
149
|
+
|
|
150
|
+
s.start() # Matches the start of a line.
|
|
151
|
+
# There is no `simply.not_start()` function.
|
|
152
|
+
# Instead, use `simply.not_behind(simply.start())`.
|
|
153
|
+
|
|
154
|
+
s.end() # Matches the end of a line.
|
|
155
|
+
# There is no `simply.not_end()` function.
|
|
156
|
+
# Instead, use `simply.not_ahead(simply.end())`.
|
|
157
|
+
|
|
146
158
|
####################
|
|
147
159
|
# Custom Sets
|
|
148
160
|
####################
|
|
@@ -155,43 +167,26 @@ s.between(0, 9) # Matches any digit from 0 to 9.
|
|
|
155
167
|
s.between('a', 'z') # Matches any lowercase letter from 'a' to 'z'.
|
|
156
168
|
s.between('A', 'Z') # Matches any uppercase letter from 'A' to 'Z'.
|
|
157
169
|
|
|
158
|
-
# Matches any
|
|
159
|
-
s.
|
|
160
|
-
#
|
|
161
|
-
# Key distinctions:
|
|
162
|
-
# - simply.in_() typically processes faster than simply.any_of().
|
|
163
|
-
# - simply.any_of() can process composite patterns while simply.in_() cannot.
|
|
164
|
-
# A composite pattern is one consisting of smaller combined patterns.
|
|
165
|
-
|
|
166
|
-
####################
|
|
167
|
-
# Anchors
|
|
168
|
-
####################
|
|
169
|
-
|
|
170
|
-
s.start() # Matches the start of a line.
|
|
171
|
-
# There is no `simply.not_start()` function.
|
|
172
|
-
# Instead, use `simply.not_behind(simply.start())`.
|
|
173
|
-
|
|
174
|
-
s.end() # Matches the end of a line.
|
|
175
|
-
# There is no `simply.not_end()` function.
|
|
176
|
-
# Instead, use `simply.not_ahead(simply.end())`.
|
|
170
|
+
# Matches any provided patterns, but they can't include subpatterns.
|
|
171
|
+
s.in_chars(s.letter(), s.digit(), ',.') # Matches any letter, digit, comma, and period.
|
|
172
|
+
# A composite pattern is one consisting of subpatterns (created by constructors and lookarounds).
|
|
177
173
|
|
|
178
174
|
####################
|
|
179
175
|
# Constructors
|
|
180
176
|
####################
|
|
181
177
|
|
|
182
|
-
s.any_of() # Matches any
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
# A composite pattern is one consisting of smaller combined patterns.
|
|
188
|
-
s.any_of(pattern1, pattern2) # Matches only if one of the patterns listed is present.
|
|
178
|
+
s.any_of() # Matches any pattern, including patterns consisting of subpatterns.
|
|
179
|
+
# A composite pattern is one consisting of subpatterns (created by constructors and lookarounds).
|
|
180
|
+
pattern1 = s.merge(s.digit(3), s.letter(3)) # Matches 3 digits followed by 3 letters.
|
|
181
|
+
pattern2 = s.merge(s.letter(3), s.digit(3)) # Matches 3 letters followed by 3 digits.
|
|
182
|
+
s.any_of(pattern1, pattern2) # Matches either pattern1 or pattern2
|
|
189
183
|
|
|
190
184
|
|
|
191
185
|
s.may() # Optionally matches the provided patterns.
|
|
192
186
|
# If a `may` pattern isn't there, it still will match the rest of the patterns.
|
|
193
|
-
# In the text, "ABC2" the patter below matches ['A', 'B', 'C2']
|
|
194
187
|
s.merge(s.letter(), s.may(s.digit()))
|
|
188
|
+
# Matches any letter, and includes any digit following the letter.
|
|
189
|
+
# In the text, "AB2" the pattern above matches 'A' and 'B2'.
|
|
195
190
|
|
|
196
191
|
|
|
197
192
|
s.merge() # Combines multiple patterns into one larger pattern.
|
|
@@ -202,12 +197,53 @@ s.capture() # Creates a numbered group that can be indexed for extracting part
|
|
|
202
197
|
# Capture is used the same as merge.
|
|
203
198
|
s.capture(s.letter(), s.digit())
|
|
204
199
|
|
|
200
|
+
# Captures CANNOT be invoked with a range: s.capture(s.digit(), s.letter())(1, 2) <== INVALID
|
|
201
|
+
# Captures CAN be invoked with a number of copies: s.capture(s.digit(), s.letter())(3) <== VALID
|
|
202
|
+
|
|
203
|
+
three_digit_group = s.capture(s.digit(3))
|
|
204
|
+
four_groups_of_three = three_digit_groups(4)
|
|
205
|
+
|
|
206
|
+
example_text = "Here is a number: 111222333444"
|
|
207
|
+
match = re.search(str(four_groups_of_three), example_text) # Notice str(pattern)
|
|
208
|
+
|
|
209
|
+
print("Full Match:", match.group())
|
|
210
|
+
print("First:", match.group(1))
|
|
211
|
+
print("Second:", match.group(2))
|
|
212
|
+
print("Third:", match.group(3))
|
|
213
|
+
print("Fourth:", match.group(4))
|
|
214
|
+
|
|
215
|
+
# Output:
|
|
216
|
+
# Full Match: 111222333444
|
|
217
|
+
# First: 111
|
|
218
|
+
# Second: 222
|
|
219
|
+
# Third: 333
|
|
220
|
+
# Fourth: 444
|
|
221
|
+
|
|
205
222
|
|
|
206
223
|
s.group() # Creates a named group that can be referenced for extracting part of a match.
|
|
207
224
|
# group is used the same as merge and capture but it takes a string name as the first argument.
|
|
208
225
|
s.group('my_group', s.letter(), s.digit())
|
|
209
|
-
|
|
226
|
+
|
|
227
|
+
# Unlike merge and capture, groups CANNOT be invoked with a range.
|
|
210
228
|
# This is because group names must be unique in a pattern.
|
|
229
|
+
# s.group('unique_name', s.digit())(1, 2) <== INVALID
|
|
230
|
+
|
|
231
|
+
# Groups can easily be referenced from the match by name
|
|
232
|
+
# assuming the numbers have been grouped and named properly.
|
|
233
|
+
example_text = "Here is a phone number: 123-456-7890."
|
|
234
|
+
match = re.search(str(phone_number_pattern), example_text) # Notice str(pattern)
|
|
235
|
+
|
|
236
|
+
print("Full Match:", match.group())
|
|
237
|
+
print("Area Code:", match.group("area_code"))
|
|
238
|
+
print("Central Part:", match.group("central_part"))
|
|
239
|
+
print("Last Part:", match.group("last_part"))
|
|
240
|
+
|
|
241
|
+
# Output:
|
|
242
|
+
# Full Match: 123-456-7890
|
|
243
|
+
# Area Code: 123
|
|
244
|
+
# Central Part: 456
|
|
245
|
+
# Last Part: 7890
|
|
246
|
+
|
|
211
247
|
|
|
212
248
|
####################
|
|
213
249
|
# Lookarounds
|
|
@@ -231,10 +267,16 @@ s.merge(s.behind(s.digit()), s.letter()) # Only matches a letter preceded by a
|
|
|
231
267
|
|
|
232
268
|
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!
|
|
233
269
|
|
|
234
|
-
|
|
270
|
+
## 🌎 Locations
|
|
271
|
+
[](https://pypi.org/project/STRling/)
|
|
272
|
+
[](https://github.com/TheCyberLocal/STRling)
|
|
235
273
|
|
|
236
|
-
|
|
274
|
+
## 🌐 Socials
|
|
275
|
+
[](https://linkedin.com/in/tzm01)
|
|
237
276
|
|
|
238
|
-
|
|
277
|
+
## 💖 Support
|
|
278
|
+
If you find my content helpful or interesting, consider buying me a coffee. Every cup is greatly appreciated and fuels my work!
|
|
239
279
|
|
|
240
|
-
|
|
280
|
+
[](https://buymeacoffee.com/thecyberlocal)
|
|
281
|
+
[](https://www.paypal.com/paypalme/TheCyberLocal)
|
|
282
|
+
[](https://www.venmo.com/TheCyberLocal)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
from STRling import simply as s
|
|
2
|
+
|
|
3
|
+
class Patterns:
|
|
4
|
+
@property
|
|
5
|
+
def phone_number_US(self):
|
|
6
|
+
"""
|
|
7
|
+
Returns a pattern for matching US phone numbers.
|
|
8
|
+
|
|
9
|
+
Primary match examples:
|
|
10
|
+
- (123) 456-7890
|
|
11
|
+
- 123-456-7890
|
|
12
|
+
- 123 456 7890
|
|
13
|
+
- 123.456.7890
|
|
14
|
+
|
|
15
|
+
Edge-cases to consider:
|
|
16
|
+
- (123-456.7890
|
|
17
|
+
- 123).456 7890
|
|
18
|
+
- (123 456-7890
|
|
19
|
+
|
|
20
|
+
Named groups:
|
|
21
|
+
- 'first': The first set of 3 digits.
|
|
22
|
+
- 'second': The second set of 3 digits.
|
|
23
|
+
- 'third': The last set of 4 digits.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
- Pattern: A Pattern object for matching US phone numbers.
|
|
27
|
+
"""
|
|
28
|
+
first = s.group('first', s.digit(3))
|
|
29
|
+
second = s.group('second', s.digit(3))
|
|
30
|
+
third = s.group('third', s.digit(4))
|
|
31
|
+
first_sect = s.merge(s.may('('), first, s.may(')'))
|
|
32
|
+
sep = s.in_chars('-. ')
|
|
33
|
+
phone_number = s.merge(first_sect, sep, second, sep, third)
|
|
34
|
+
phone_number_pattern = s.merge(s.not_behind(s.digit()), phone_number, s.not_ahead(s.digit()))
|
|
35
|
+
return phone_number_pattern
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def ipv4(self):
|
|
39
|
+
"""
|
|
40
|
+
Returns a pattern for matching IPv4 addresses.
|
|
41
|
+
|
|
42
|
+
Primary match examples:
|
|
43
|
+
- 192.168.1.1
|
|
44
|
+
- 10.0.0.1
|
|
45
|
+
- 172.16.254.1
|
|
46
|
+
- 255.255.255.255
|
|
47
|
+
- 0.0.0.0
|
|
48
|
+
|
|
49
|
+
Edge-cases to consider:
|
|
50
|
+
- 999.999.999.999
|
|
51
|
+
- 256.256.256.256
|
|
52
|
+
- 192.168.1.256
|
|
53
|
+
|
|
54
|
+
Captured groups:
|
|
55
|
+
- Numbered 1 to 4 for each octet.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
- Pattern: A Pattern object for matching IPv4 addresses.
|
|
59
|
+
"""
|
|
60
|
+
ipv4_pattern = s.capture(
|
|
61
|
+
s.digit(1, 3), '.',
|
|
62
|
+
s.digit(1, 3), '.',
|
|
63
|
+
s.digit(1, 3), '.',
|
|
64
|
+
s.digit(1, 3)
|
|
65
|
+
)
|
|
66
|
+
return ipv4_pattern
|
|
67
|
+
|
|
68
|
+
patterns = Patterns()
|