CH04-L03 · applied

Pattern checks with regular expressions

Validate a fixed code format and learn when a simpler string method is clearer.

General CELRuns hereDocumented
What will I learn?

You can use matches() for a format without confusing it with a plain-text search.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

matches() tests text against an RE2-style regular expression supplied by the CEL implementation. ^ anchors the start, $ anchors the end, [A-Z]{2} requires two uppercase ASCII letters, and [0-9]{4} requires four digits. The hyphen between them is literal.

Read the prepared example

The CEL + JSON rule is:

code.matches('^[A-Z]{2}-[0-9]{4}$')

Its input is:

{
  "code": "SK-2026"
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. SK satisfies the first character class and count.
  2. - occurs in the required position.
  3. 2026 satisfies the four-digit requirement, so the result is true.

Your task

  1. Run the valid code.
  2. Change it to sk-2026.
  3. Change it to SK-26, then explain which part of the pattern rejects each value.

Expected observation: Both modified values return false: one violates case, the other violates digit count.

Common mistake

A regular expression that is hard to explain is hard to maintain. Prefer named string checks for simple prefix, suffix, or fragment rules.

Show the explanation

The pattern could match a suitable fragment inside a longer string rather than the complete value.

Knowledge check

What would happen if the start and end anchors were removed?

Key takeaway

Use matches() for genuine formats, anchor full-value rules, and explain every pattern component.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.