Set Regular Expression Options

A mistake many people new to regular expressions make is to take any regex at face value. A regular expression is meaningless in isolation. It will be interpreted by a particular application or programming library. Different tools interpret regular expressions differently. Sometimes the differences are subtle. Sometimes various tools use completely different syntax.

On top of that, most regex engines support various matching modes or options, such as case sensitivity. A regular expression written with the “case insensitive” option in mind will miss most of the intended matches if you forget to turn on that option when you implement the regular expression.

Fortunately, RegexBuddy makes things easy for you. When using RegexBuddy’s library of regular expressions, click the Use button rather than copying and pasting the regex, and RegexBuddy will move over the application and mode selections along with the regex. When converting a regex from one application to another, RegexBuddy takes any differences in supported matching modes into account, and adjusts the regex or warns as needed.

When you implement your regular expression on the Use panel, RegexBuddy will automatically set the same matching options in the source code snippets. If you copy and paste the regular expression manually into your source code, the matching options will not be copied over automatically, since they’re not part of the regular expression itself. Make sure to set the options in your own source code.

You can set the regular expression options by selecting different values the drop-down lists on the toolbar above the regular expression in RegexBuddy. The value shown in each drop-down list is the present state of that option. You can also toggle the options by pressing Alt+O on the keyboard and then pressing the underlined letter of the option you want to toggle as it appears in the popup menu. The popup menu uses check marks to indicate which options are on.

By default, RegexBuddy only shows the options that the selected application actually allows you to change. On the Operation tab in the Preferences you can choose to always show all the options that RegexBuddy supports. The above screen shot illustrates this. The options that the active application doesn’t support have only one item in the drop-down list, so you can see the state of the option, but not change it. In the Alt+O popup menu, options that can’t be changed are grayed out. The options for splitting strings are never shown in Match or Replace mode, even when you turned on the preference to show all options.

Default Options

The drop-down lists use positive labels to indicate all states. The case sensitivity or insensitivity option, for example, is always indicated as “case sensitive” and “case insensitive”. This avoids confusing double negations like “case insensitive off”. It also avoids confusion when switching between applications where one is case sensitive by default with an option to make it case insensitive, and the other is case insensitive by default and has an option to make it case sensitive.

But if the potential confusion of toggle buttons does not bother you, you choose to “show options that can be changed with toggle buttons that indicate the “on“ state” on the Operation tab in the Preferences. The above screen shot shows this. The benefit is that the buttons need only one click to toggle an option while drop-down lists require two. The actual application you will use the regex with likely also has toggle buttons or checkboxes rather than drop-down lists. But if you switch between applications in RegexBuddy, you will need to take into account that the meaning of the buttons may invert. If an application is case sensitive by default, the button will say “case insensitive”. If you then switch to an application that is case insensitive by default, the button will change its label to “case sensitive” and will toggle its state (depressed or not). By changing their labels the buttons ensure that the application’s default options are being used when all buttons are off (not depressed).

Regardless of whether options are shown with drop-down lists or toggle buttons, you can click the Reset button or select Reset All Options in the popup menu to change all options to the defaults for the current application.