Define a Match, Replace, or Split Action

With most tools and languages, you can perform three actions using a regular expression: match, replace or split. You can define, test, debug and implement all three with RegexBuddy.

Match

A match action applies the regular expression pattern to a subject string, trying to find a match. If successful, the match can be applied again to the remainder of the subject string, to find subsequent matches. When you perform a search using a regular expression in a text editor, you are technically executing a match action. The file you are editing is the subject string.

When programming, you can use match actions to validate user input and external data. The regex \A-?\d\Z, for example, checks whether an integer number was entered. Match actions make it easy to parse and process data. Use capturing groups to extract just the data you want.

To define a match action in RegexBuddy, click on the Match button near the top of the RegexBuddy window. Enter the regular expression into the text box. You can right-click the text box to insert regex tokens without having to remember their exact syntax.

Replace

A replace action applies the regular expression pattern to a subject string, and replaces the match or each of the matches with a replacement string. Depending on the tool or language used, this will either modify the original subject string, or create a new string with the replacements applied. By using backreferences in the replacement text, you can easily perform some pretty complex substitutions. To invert a list of assignments, for example, turning one = another into another = one, use the regex (\w+)\s*=\s*(\w+) and replace with $2 = $1.

To define a replace action in RegexBuddy, click on the Replace button at the top. Enter the regular expression in the topmost text box, and the replacement text into the text box just below. To easily insert a backreference into the replacement text, right-click in the text box with the replacement text at the spot where you want to insert the backreference. This will move the text cursor to that position and show the context menu. Move the mouse to the Insert Token item to expand it. Finally, select Use Backreference.

Some applications do not have the ability to search-and-replace. The Replace button is grayed out when you select such an application.

Split

A split action creates an array or list of strings from a given subject string. The first string in the resulting array is the part of the subject before the first regex match. The second string is the part between the first and second matches, the third string the part between the second and third matches, etc. The final string is the remainder of the subject after the last regex match. The text actually matched by the regex is discarded.

In RegexBuddy, simply click on the Split button at the top and enter the regular expression into the text box. You can right-click the text box to insert regex tokens without having to remember their exact syntax.

Many applications allow you to specify a limit for the number of times the string should be split. Some allow you to choose whether text matched by capturing groups should be added to the array and whether empty strings may be added to the array. Those options will appear after the regular expression options when RegexBuddy is in Split mode.

Some applications do not have the ability to split strings. The Split button is grayed out when you select such an application.