Skip to content

Matching basics

Amy Brennan-Luna edited this page Aug 6, 2020 · 5 revisions

BEX Matching is a structured code matching library to assist with finding and replacing code

You're likely already familiar with the idea of structured code.

For example,

if (value == 1) {
  System.out.println("You're #1");
}

You'll likely recognize that as an if branch. How do you know it's an if branch? Well, it has the structure of an if branch.

if (something) { stuff }

The structure focuses on the big picture and gives flexibility for the details. For example, the braces may be on the same line as the if condition; they may be on different lines. The code could be indented with spaces, tabs, or they may be no indention.

Imagine writing a regular expression to parse this out. You'd have to deal with one big challenge - balanced parenthesis and braces. Regular expressions were designed for text, not code. This is what makes structured code matching unique; it handles ensuring everything is balanced, without you having to tell it.

To describe the above if branch, you'd use the following pattern

if (:[something]) { :[stuff] }

Notice how the above pattern looks just like the structure we mentioned earlier.

Clone this wiki locally