Skip to the page
Najova

Docs

/

Guides

Patterns

A pattern describes the shape of some text, like "four digits, a dash, two digits". Use one to find dates, check what someone typed or tidy text up.

Finding text

$findPattern gives every part of the text that fits the pattern. Each one is a table: its text, where it starts (at, the first letter is 1) and its groups, what each pair of brackets ( ) held.

dates.naja
$let[notes;Rye on 2026-12-03, bagels on 2026-12-10 and cake on 2026-12-24.]
$each[m;$findPattern[$get[notes];(\d{4})-(\d\d)-(\d\d)]]
  $print[$get[m.text] at letter $get[m.at]: day $item[$get[m.groups];3]]
$endeach
$print[$count[$findPattern[$get[notes];\d{4}-\d\d-\d\d]] dates]

› naja run dates.naja

2026-12-03 at letter 8: day 03
2026-12-10 at letter 30: day 10
2026-12-24 at letter 53: day 24
3 dates

The letters of a pattern

Most letters stand for themselves. These stand for more, with the same meanings as regular expressions in other languages:

In a patternFits
.Any one letter
\d, \w, \sA digit, a word letter (a to z, digits and _), a space or new line
[aeiou], [a-z], [^0-9]One of these letters, one in this range, anything but these
*, +, ?What's before it: any number of times, once or more, or maybe once
{3}, {2,4}Exactly 3 times, or 2 to 4 times
a|bOne or the other
( )A group: kept, so you can use it again
^, $The start and end of the text
\bThe edge of a word
(?i)At the start: upper and lower case are the same

Put a \ before a letter to mean just that letter: \. is a full stop, not any letter. A pattern with [ or ] in it goes in quotes, like "[0-9]+", so Najova doesn't read the brackets as its own.

Checking what was typed

With ^ at the start and $ at the end, the whole text has to fit, not just part of it. That makes a pattern a check: if it finds nothing, the text isn't the right shape.

check.naja
$-- ^ and $ make the whole text fit, not just a part of it.
$function[isEmail;t]
  $let[found;$findPattern[$get[t];"^[^@\s]+@[^@\s]+\.\w+$";1]]
  $return[$count[$get[found]] == 1]
$endfunction

$each[t;$list[sam@example.com;sam@;ada at work;lee@shop.co.uk]]
  $if[$isEmail[$get[t]]]
    $print[$get[t]: looks right]
  $else
    $print[$get[t]: not an email]
  $endif
$endeach

› naja run check.naja

sam@example.com: looks right
sam@: not an email
ada at work: not an email
lee@shop.co.uk: looks right

The 1 after the pattern stops at the first part that fits: one is enough to know.

Changing text

$replacePattern changes every part that fits. In the new text, {1}, {2} and so on are what each group held, and {0} is the whole part, so you can keep some of it and move it around.

tidy.naja
$let[t;Call Sam on 0412 345 678 or Ada on 0498 765 432.]
$print[$replacePattern[$get[t];"04\d\d \d{3} (\d{3})";04xx xxx {1}]]
$print[$replacePattern[too    many   spaces;\s+;" "]]
$print[$replacePattern[smith, sam;(\w+), (\w+);{2} {1}]]

› naja run tidy.naja

Call Sam on 04xx xxx 678 or Ada on 04xx xxx 432.
too many spaces
sam smith

The single space is in quotes, because spaces at the ends of a value are dropped.

As little as fits

*, + and ? take as much as they can. Put a ? after them, like .+?, and they take as little as they can instead. That's the difference between one long match and each <b> on its own.

lazy.naja
$let[t;<b>Rye</b> and <b>Sourdough</b>]
$let[all;$item[$findPattern[$get[t];<b>.+</b>];1]]
$print[As much as fits: $get[all.text]]
$each[m;$findPattern[$get[t];<b>(.+?)</b>]]
  $print[As little as fits: $item[$get[m.groups];1]]
$endeach
$print[$count[$findPattern[RYE and rye;(?i)rye]] found]

› naja run lazy.naja

As much as fits: <b>Rye</b> and <b>Sourdough</b>
As little as fits: Rye
As little as fits: Sourdough
2 found

Good to know

Patterns are Najova's own, built in and without anything to install. A pattern that would take too long, like one that tries the same text in millions of ways, stops with a mistake instead of making the script hang. To cut text at one letter or word, $split and $replace are simpler and quicker.