Guided learning10 minutes · Beginner

STRling quickstart

Build and match an anchored phone-number Pattern using the legacy TypeScript and Python APIs.

Install the legacy tutorial versions

This lesson teaches the legacy TypeScript 3.0.0 and Python 2.5.9 APIs for readers maintaining earlier code. For new work, see the current package directory and the Fourth Edition TypeScript compilation walkthrough.

Use the matching TypeScript release to reproduce the examples.

npm install @strling-lang/strling@3.0.0

Use the matching legacy Python release from PyPI.

python -m pip install STRling==2.5.9

For Fourth Edition Python installation and the current compiler API, follow the Python binding guide.

Build your first TypeScript Pattern

Create a phone-number shape with three captured fields and optional separators:

import { simply as s } from '@strling-lang/strling';

const phone = s.merge(
  s.start(),
  s.capture(s.digit(3)),
  s.may(s.anyOf('-', '.', ' ')),
  s.capture(s.digit(3)),
  s.may(s.anyOf('-', '.', ' ')),
  s.capture(s.digit(4)),
  s.end(),
);

Read it from top to bottom:

  1. start at the beginning;
  2. capture three digits;
  3. allow one optional separator;
  4. capture three digits;
  5. allow another optional separator;
  6. capture four digits;
  7. stop at the end.

Generate and use the RegEx

Convert the Pattern to a string and give it to the built-in runtime:

const regex = new RegExp(String(phone));

console.assert(regex.test('555-123-4567'));
console.assert(regex.test('555 123 4567'));
console.assert(!regex.test('call 555-123-4567 now'));

The legacy JavaScript example produces this RegEx shape.

^(\d{3})(?:(?:-|\.|\ ))?(\d{3})(?:(?:-|\.|\ ))?(\d{4})$

The legacy expression rejects surrounding text in this fixture. Its anchors follow JavaScript’s rules for the chosen flags. The Fourth Edition exposes strict input positions for full-value checks.

The same concept in Python

Python uses snake_case where the host language expects it:

import re
from STRling import simply as s

phone = s.merge(
    s.start(),
    s.capture(s.digit(3)),
    s.may(s.any_of("-", ".", " ")),
    s.capture(s.digit(3)),
    s.may(s.any_of("-", ".", " ")),
    s.capture(s.digit(4)),
    s.end(),
)

regex = re.compile(str(phone))
assert regex.match("555-123-4567")

The Pattern is responsible for construction. RegExp or re is responsible for matching.

Name important captures

When a field has domain meaning, use a named group:

const area = s.group('area', s.digit(3));
const exchange = s.group('exchange', s.digit(3));
const line = s.group('line', s.digit(4));
const namedPhone = s.merge(area, '-', exchange, '-', line);

Names must be unique within the final Pattern. Use numbered captures for repeated structures.

Make one piece reusable

Move the separator into a named value:

const separator = s.anyOf('-', '.', ' ');
const optionalSeparator = s.may(separator);

That is the central STRling habit: isolate a requirement, name it, then compose it.

Next steps