搜索插件、代码仓与 APP 分类

插件详情v1.0.0

recursive_regex

一个 RegExp 的实现,用于隔离文本中的定界块,并分别对每个块应用该模式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.3万
喜欢数
10
Pub 分数
130

安装插件

flutter pub add recursive_regex

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,扩展了 RegExp 功能,以递归方式解析和处理用分隔符标记的文本块,例如嵌套括号或自定义分隔符。它对每个独立的文本块应用模式,从而实现复杂字符串中结构化文本的精确提取和转换。非常适合解析嵌套语法,如模板、表达式或标记语言。该实现支持多种平台,包括移动设备、网页和桌面端,适用于需要高级文本操作的跨平台应用程序。

适合场景

  • 解析嵌套模板
  • 从分隔块中提取内容
  • 使用递归处理结构化文本
  • 实现自定义 DSL
  • 处理代码中的嵌套表达式
  • 多层字符串中的文本转换

主要特点

  • 递归块隔离
  • 每个区块独立的模式应用
  • 支持自定义分隔符
  • 跨平台兼容性
  • 简单的 API 集成
  • 高效的文本处理

适合对象

  • 开发模板引擎的开发者
  • 需要嵌套文本解析的工具
  • 具有结构化字符串数据的应用程序
  • DSL 或语言解析器实现
  • 文本编辑器或代码分析器

使用建议

  • 可能无法高效处理深度嵌套的结构
  • 需要仔细设计分隔符
  • 性能取决于输入的复杂性
  • 限制为基于文本的模式
  • 不适用于非递归解析需求
  • 极端嵌套可能导致栈溢出

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

recursive_regex

pub package

An implementation of Dart's RegExp class that isolates delimited blocks of text and applies the delimited pattern to each block separately.

The [RecursiveRegex] class implements Dart's [RegExp] class. As such, it has all of the same methods as [RegExp] and can be used interchangeably with [RegExp]s.

Usage

import 'package:recursive_regex/recursive_regex.dart';

[RecursiveRegex] has 2 required parameters, [startDelimiter] and [endDelimiter]. Both accept a [RegExp], must not be null, and must not have identical patterns.

final regex = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
);

This [RecursiveRegex], if applied to a string, would capture every block of text delimited by the < and > characters.

final input = 'a<b<c<d><e>f>g>h';
regex.allMatches(input); // ['<b<c<d><e>f>g>']

By default, only top-level blocks of delimited text are matched. To match every block of delimited text, nested or not, [global] can be flagged true.

final input = '<a<b<c><d>>>';

final regex = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  global: true,
);

regex.allMatches(input); // ['<c>', '<d>', '<b<c><d>>', '<a<b<c><d>>>']

Delimited blocks of text are matched in the order that they're closed.

Note: [RecursiveRegex] is not optimized for parsing strings that do not contain nested blocks of delimited text, [RegExp] would be more efficient if you know the string doesn't contain any nested delimiters.

[prepended] & [appended] Patterns

[Pattern]s can be provided to [RecursiveRegex]'s [prepended] and [appended] parameters to disqualify any matched set of delimiters that aren't lead and/or followed by the provided [Pattern].

final input = '0<a<b<1<c>2>3<d>>>';

final prepended = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  prepended: RegExp(r'[0-9]'),
  global: true,
);

prepended.allMatches(input); // ['1<c>', '3<d>', '0<a<b<1<c>2>3<d>>>']

final appended = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  appended: RegExp(r'[0-9]'),
  global: true,
);

appended.allMatches(input); // ['<c>2', '<1<c>2>3']

final both = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  prepended: RegExp(r'[0-9]'),
  appended: RegExp(r'[0-9]'),
  global: true,
);

both.allMatches(input); // ['0<a<b<1<c>2>3']
inverseMatch

A [Pattern] can be provided to the [inverseMatch] parameter to define a pattern that should be matched, but only if it's not delimited.

Note: [global] has no affect on the matches if an [inverseMatch] pattern is provided, as all lower-level matches are delimited.

final input = 'a<b<c<d><e>f>g>h';

final regex = RecursiveRegex(
  startDelimiter: RegExp(r'<'),
  endDelimiter: RegExp(r'>'),
  inverseMatch: RegExp(r'[a-z]'),
  global: true,
);

regex.allMatches(input); // ['a', 'h']

Capture Groups

The block of text captured between the delimiters can be captured in a named group by providing [RecursiveRegex] with a [captureGroupName].

final input = '<!ELEMENT [ some markup ]>';

final regex = RecursiveRegex(
  startDelimiter: RegExp(r'<!(?<type>\w*)\s*\['),
  endDelimiter: RegExp(r']>'),
  captureGroupName: 'value',
);

print(regex.firstMatch(input).namedGroup('value')); // ' some markup '

// Groups named within the delimiters can also be called.
print(regex.firstMatch(input).namedGroup('type')); // 'ELEMENT'

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。