Marking Fields on The Samples Panel

This section describes exactly how RegexMagic operates when you mark fields on the Samples panel. If you’re new to RegexMagic, you may find this description incomprehensible. Instead, you should try out some of the examples. You’ll find that marking samples is quite intuitive with just a little bit of practice.

If you’re already familiar with RegexMagic and you want to learn exactly how the buttons on the Samples panel operate, read on. There are two basic ways to mark fields: adding New fields with the Mark button, and providing additional samples for existing fields with the colored buttons that appear once you’ve used the Mark button. Depending on which text you select and which buttons you click, RegexMagic can modify your regular expression in five ways in response to your commands. Click on the links in the text below to see screen shots.

Marking Adjacent Fields Expands Your Regular Expression

To build up a regular expression by marking fields, select the first bit of text that the regular expression should match, and click the Mark button. The text you selected becomes highlighted in yellow, to indicate it is marked as field 1 as shown below. The field is added to the Match panel, where you can change its settings if you want. In the screen shots on this page, we’ve also entered the text we marked as the name for each field to make the relation between the Samples and Match panels easier to see. RegexMagic does not automatically name fields. Fields don’t need to have names. The names are just for your reference.

Marked field 1

To expand your regular expression, select a bit of text after and adjacent to the first field and click the Mark button again. This tells RegexMagic to add field 2 right after the first one, like this:

Marked field 2

Repeat this to add a third field or as many as you like.

Marked field 3

You can also work from right to left. If you first mark “Jake” as field 1 and then mark “Mary” then RegexMagic inserts a new field 1 for “Mary” and “Jake” becomes field 2. If you then mark “John” then RegexMagic inserts yet another field 1 and the other two fields have their numbers incremented by one. The end result is the same as the screen shot above.

Marking Additional Samples Refines Patterns

Each time you click the Mark button, a new colored button appears above the sample text. Use these colored buttons when you want to mark additional sample text for an existing field, rather than add new fields as the Mark button does. If you’ve clicked the Mark button 3 times to mark one regex match that should consist of 3 fields, for example, then you can then use buttons 1, 2, and 3 to mark the text that should matched by the second regex match. You can continue selecting bits of text and clicking these 3 buttons to mark additional regex matches.

Whenever you mark more text as an existing field, RegexMagic refines the pattern for that field. Depending on the text you’re working with, it may be easier to mark more text on the Samples panel to refine the pattern than to manually configure the pattern on the Match panel.

When you use the colored buttons to mark additional samples, it’s your responsibility to do so in a consistent manner. If your regex consists of 3 pattern fields, then you always need to mark the fields 1, 2, and 3 in that order. RegexMagic can’t adapt your regular expression if you contradict yourself.

Marking Non-Adjacent Fields Creates Alternation

If you don’t mark the fields adjacent and in order, RegexMagic tries to be clever about how it adds the new field to your regular expression. Let’s restart our example with a single marked field:

Marked field 1

If you now select some non-adjacent text and click the Mark button then the previously marked text becomes field 2, and the newly marked text becomes field 3. Both will be inside an alternation field 1 that has no marked text. The alternation field allows your regular expression to match either the pattern selected for field 2, or the pattern selected for field 3. When using such a regex with a “find all” command, it will find both pieces of text that you marked.

Marked a non-adjacent field

If you were to use the Mark button on any more pieces of text not adjacent to anything you’ve marked, those would become additional alternatives 4, 5, 6, etc. under the alternation field 1.

Skipping Fields Creates Alternation

Let’s go back to the second step of the original example with two pattern fields 1 and 2.

Marked field 2

You can now use button 1 to mark a bit of text that’s not adjacent to the text you just marked. This provides a second example for field 1 to start the second regex match. In this case the second example has the same text as the first, but it doesn’t need to be. It just needs to fit the same pattern for field 1. RegexMagic may adjust that pattern when you mark additional samples for an existing field.

Marked two samples for field 1

If you then select a text adjacent to the text marked with button and click the Mark button, then the Mark button does not mark the text as field 2. Had that been what you wanted then you should have clicked button 2 instead of the Mark button. The colored buttons add samples to existing fields. The Mark button always adds a new field, like this:

Marked a second field after field 1

So you’re marking a brand new field immediately adjacent to field 1. This tells RegexMagic that your regex should either match field 1 followed by field 2 (the first sample you marked), or it should match field 1 followed by the new field. To accomplish this, RegexMagic rearranges your regular expression. Field 1 stays as it is. Field 2 becomes an alternation field. The original field 2 is renumbered to 3 and becomes the first alternative under field 2. The newly marked text becomes field 4, as the second alternative under field 2.

Continuing After Skipped Fields Creates Sequences

Continuing from the example in the previous section, suppose you select some text immediately adjacent to the text marked as field 4 and click the Mark button. This tells RegexMagic that the regex should match the new field immediately after field 4. But that field is inside the alternation field 2, so RegexMagic can’t just stick the new field at the end of your regex. It needs to specify that field 2 consists of two alternatives. The first alternative is field 3, and the second alternative consists of two fields: field 4 followed by the newly marked field.

To do this, RegexMagic changes field 4 into a sequence field, and the old field 4 becomes field 5 and the first field in the sequence. The newly marked field becomes field 6 and the second field in the sequence.

Marked a second field after field 4

Marking Overlapping Fields Creates a Sequence

If you select text that is already part of a field and you click the Mark button then RegexMagic splits the existing field. If you include the first character of the existing field in your selection then the new field is inserted before the existing field. If you include the last character in your selection then the new field is inserted after the existing field. If you do not include the first and last character of the existing field then two new fields are inserted after the existing field. The original field keeps the previously marked text before the newly marked selection. The first added field takes the selected text. The second added field takes the previously marked text after the newly marked selection. In any case, the pattern of the existing field is refined to take into account that it now supposed to match less of the text.

If the existing field was inside an alternation field which matches one of many fields then RegexMagic replaces that field with a new sequence field. This new sequence then holds the existing field and the one or two newly added fields. This way, the text that was originally marked for a single field and is now marked for two or three fields is still always matched as a complete sequence.