搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。