Skip to content

Commit f8740a5

Browse files
committed
Add skeleton regex builder GUI, add new funcs
- Added RegexEscape to get the same functionality as re.escape in Python - Added RegexFindObject to make it easy to find a single match object for a regex that matches in multiple places - Added a simple GUI which I will eventually make work so that it can be used to design regular expressions.
1 parent a77f00d commit f8740a5

6 files changed

+37
-9
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### To Be Added
1010

11-
- ???
11+
- Fully functional Regex Builder Form.
12+
13+
# [2.0.0] - 2022-03-10
14+
15+
### Added
16+
17+
- RegexFindObject (like indexing in `list(re.finditer(x))`, which returns the n^th match as a match object, unlike RegexFind, which returns a string.
18+
- RegexEscape (`re.escape` workalike), which takes a string and returns the string with all special characters escaped.
19+
- The basic skeleton of "Regex Builder Form.xlsm", which will allow people to build regular expressions by a GUI similar to [RegexBuddy](https://www.rexegg.com/regexbuddy-tutorial.html). This is not yet functional.
1220

1321
## [1.0.0] - 2022-03-09
1422

Regex Builder Form.xlsm

20.8 KB
Binary file not shown.

RegexFunctions.bas

+28-8
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,42 @@ RegexFindAll = Join(strings, sep)
119119
End Function
120120

121121

122-
Function RegexFind(pat As String, _
122+
Function RegexFindObject(pat As String, _
123123
str As String, _
124124
Optional match_num As Integer = 0, _
125-
Optional submatch_sep As String = "", _
126125
Optional ignore_case As Boolean = False, _
127-
Optional multiline As Boolean = False) As String
126+
Optional multiline As Boolean = False) As Object
128127
' Get the match_num^th match in string str to a regex with various params.
129128
Dim is_global As Boolean
130129
Dim matches
131-
Dim num_submatches As Integer
132130
is_global = IIf(match_num = 0, False, True)
133131
Set matches = RegexMatches(pat, str, is_global, ignore_case, multiline)
134132
If matches.Count - 1 < match_num Then
135133
MsgBox ("Match number " + match_num _
136134
+ " couldn't be found in matches of length " + matches.Count)
137135
Exit Function
138136
End If
139-
num_submatches = matches.Item(match_num).SubMatches.Count - 1
137+
Set RegexFindObject = matches.Item(match_num)
138+
End Function
139+
140+
141+
Function RegexFind(pat As String, _
142+
str As String, _
143+
Optional match_num As Integer = 0, _
144+
Optional submatch_sep As String = "", _
145+
Optional ignore_case As Boolean = False, _
146+
Optional multiline As Boolean = False) As String
147+
' Get the match_num^th match AS A STRING in string str to a regex with various params.
148+
' If there are any capture groups in the match, they will be separated by submatch_sep.
149+
Dim match: Set match = RegexFindObject(pat, str, match_num, ignore_case, multiline)
150+
num_submatches = match.SubMatches.Count - 1
140151
If num_submatches < 0 Then
141-
RegexFind = matches.Item(match_num).Value
152+
RegexFind = match.Value
142153
Else
143154
ReDim strings(num_submatches) As String
144155
Dim ii As Integer
145156
For ii = 0 To num_submatches
146-
strings(ii) = matches.Item(match_num).SubMatches.Item(ii)
157+
strings(ii) = match.SubMatches.Item(ii)
147158
Next ii
148159
RegexFind = Join(strings, submatch_sep)
149160
End If
@@ -160,9 +171,12 @@ Function RegexSplit(pat As String, _
160171
' if there are capturing groups in the regex, each capturing group gets its
161172
' own element in the string list at the location where it's found in the string
162173
' in addition to all the substrings not matched by the regex
174+
' If num_splits is -1, split out every instance of the regex that is found
175+
' If num_splits is greater than 0, split at most num_splits times before stopping.
176+
' num_splits cannot be 0 or a negative number other than 1.
163177
Dim is_global As Boolean
164178
Dim matches
165-
is_global = IIf(num_splits = 0, False, True)
179+
is_global = IIf(num_splits = 1, False, True)
166180
Set matches = RegexMatches(pat, str, is_global, ignore_case, multiline)
167181
ReDim out(1) As Variant
168182
If matches.Count = 0 Then
@@ -214,3 +228,9 @@ strings = RegexSplit(pat, str, ignore_case, multiline, num_splits)
214228
RegexSplitToString = Join(strings, sep)
215229
End Function
216230

231+
232+
Function RegexEscape(str As String) As String
233+
' Escape all the special characters in string str
234+
Dim special_chars As String: special_chars = "([\[\]\(\)\{\}\?\+\*\.\^\$\|\\])"
235+
RegexEscape = RegexReplace(special_chars, "\$1", str, True, False, True)
236+
End Function

RegexFunctions.xlam

6.54 KB
Binary file not shown.

regexfunctions_addin_test.xlsx

71 Bytes
Binary file not shown.

successful_test_outcomes.PNG

4.99 KB
Loading

0 commit comments

Comments
 (0)