This example demonstrates how you can make part of your regular expression repeat itself to match text of variable length, even if the repeated part is complex (meaning there’s no single RegexMagic pattern to match it). You can find this example as “Fields: sequence” in the RegexMagic library.
For this example, we’ll create a regex that verifies if a string holds a special kind of code. The code is delimited with angle brackets. Between those are 3 to 6 numbers ranging from 127 to 67301. The numbers are delimited by colons.
The description in the preceding paragraph is what you might read in a specification. Regular expressions can’t be written that way. Regular expressions work from left to right. Rewriting the spec from left to right, we have an opening angle bracket, followed by a number between 127 and 64301, followed by 2 to 5 times a colon and a number between 127 and 64301, followed by a closing angle bracket. While our rewritten spec is long-winded and perhaps less clear, writing it out or thinking it through this way makes it much easier to build the regular expression.
<127:7898:1983> <1234:789:18988:33891:9819> <333:4444:55555:67301:1289:1081>
. RegexMagic automatically detects the correct “literal text” pattern for this field.
. RegexMagic detects the “integer” pattern that we want, but not with all the right options as those can’t be guessed from marking just one number.
. RegexMagic detects it as matching a literal colon.
, in the “kind of field” drop-down list, select “sequence”. This tells RegexMagic we want to repeat multiple fields as one unit. The old field
that matches a literal colon is moved into the sequence as field
.
to 2 and 5.
to add field
as the second field in the sequence under field
.
. This makes field
match an integer between 127 and 67301 just as field
does, though not necessarily the same integer.
to make the field buttons work on that field.
to add field
after field
, without making it a part of the sequence under field
. Make sure not to confuse this button with the Add Last Sub-Field button
which would make the new field part of the sequence.
should match.
# 1. Literal text < # 2. Integer (?:6730[01]|67[0-2][0-9]{2}|6[0-6][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{3}|[2-9][0-9]{2}|1[3-9][0-9]|12[7-9]) # 3. Fields 4 to 5 in sequence (?: # 4. Literal text : # 5. Same as field 2: Integer (?:6730[01]|67[0-2][0-9]{2}|6[0-6][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{3}|[2-9][0-9]{2}|1[3-9][0-9]|12[7-9]) ){2,5} # 6. Literal text >
Required options: Free-spacing.
Unused options: Case sensitive; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.
and the integers after them as field
. No text is highlighted as field
. That’s because the highlighting indicates which pattern matched which text. Field
doesn’t match any text on its own. Its only purpose is to repeat fields
and
together.
<127:7898:1983> <1234:789:18988:33891:9819> <333:4444:55555:67301:1289:1081>