Skip to content
pubscout

string_scanner

1.4.1, released 2 years ago by tools.dart.dev

A class for parsing strings using a sequence of patterns.

WASM-readyQuiet but busy
$ flutter pub add string_scanner
Source repository

What it does

Exposes a StringScanner type that makes it easy to parse a string using a series of Patterns. It allows you to move scan pointers, match expected patterns, and handle errors during string parsing.

AI-written summary of version 1.4.1, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to build a custom string parser using patterns or regular expressions.
  • You need detailed position tracking and error formatting while parsing text inputs.

Skip it when

  • You are parsing heavily structured formats like XML or YAML where dedicated parsers already exist.

Strengths

  • Simplifies sequential string parsing with patterns.
  • Provides helpful methods for expectations and matching errors.

Weaknesses

  • Requires manual imperative parsing logic for complex grammars.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
1
import 'dart:math' as math;
import 'package:string_scanner/string_scanner.dart';

num parseNumber(String source) {
  final scanner = StringScanner(source);
  final negative = scanner.scan('-');
  scanner.expect(RegExp(r'\d+'));
  var number = num.parse(scanner.lastMatch![0]!);
  scanner.expectDone();
  return (negative ? -1 : 1) * number;
}

Alternatives

Packages that do the same job and could replace string_scanner.

  • petitparser

    If you prefer a combinator-based parsing library over imperative scanning.

  • xml

    If you specifically need to parse XML documents.

Works with

Packages commonly used together with string_scanner: ones it builds on, ones built on it, and its own family.