Enchanted regex
Dart regex api is a good solid base of a modern regex implementation. But it still lacks some more complex functions (such as better manipulating groups) and some auxiliary functions. Because of these "problems", this package was created.
• 🔗 Group manipulation
The auxiliar funcions and extensions that will help us to manipulate and map the named group in a regex match.
String extensions
• 🌟 About FindedGroup
class.
✨ This class will be used in the main functions of this package.
This is the big star
of this package. A class that delivers far away more information about the named group match then normal Match
or RegExpMatch
classes. Dart already have named groups. But default funcions are really limited. We don't now when the match of a named group starts/ends. We only now it's content - don't even now what of the named group that content represents (there can be a lot of named groups in each string). For that reason, a lot of the funcions that malipulate will return a more complex object for each match. With more data about that match. That model is the FindedGroup
.
Atributes
With FindedGroup class you have access to the following parameters:
- FindedGroup.name:
The name of the group founded. A.ka. the name of the group in the regex, what stays inside the<>
. - FindedGroup.content:
The content string of the named group founded. Does not include the data in negative lookahead/lookbehind. - FindedGroup.fullMatchText:
The full match of the named group. Not only the group content, but the full match of the regex. - FindedGroup.globalStart:
Where theregex match
starts.
⚠️ Note: This is not the index where the group starts, but where the regex match starts. Thoose values, where the match stats and where it ends, are inside theFindedGroup.start
andFindedGroup.end
variables.
So, this is equivalent to default dartMatch.start
method. - FindedGroup.globalEnd:
Where theregex match
ends.
⚠️ Note: This is not the index where the group ends, but where the regex match ends. Thoose values, where the match stats and where it ends, are inside theFindedGroup.start
andFindedGroup.end
variables.
So, this is equivalent to default dartMatch.end
method. - FindedGroup.start:
This is the index where the group starts inside the the regex match. Note, this value is not the index where the regex match starts, but where thegroup starts
.
For example: Maybe you are mapping diferent groups inside the same regex match, so, this value will be the start index of this group inside the regex match. - FindedGroup.end:
This is the index where the group ends inside the the regex match. Note, this value is not the index where the regex match ends, but where thegroup ends
.
For example: Maybe you are mapping diferent groups inside the same regex match, so, this value will be the end index of this group inside the regex match.
Functions
- FindedGroup.copyWith():
Will create a exact copy changing only the parameters you passed in the copyWith function.
• 🔧 Auxiliar funcions
Functions that will help you with extra features with regex.
Get the text of a named group.
Get the full text founded in a RegExpMatch
or even a Match
.
PS: Yes, dart dosen't have this really simple funcion by default in it's Regex api...
final String source = 'Hello, my name is <name>';
final RegExp regex = RegExp(r'(?<name>\w+)');
final RegExpMatch? match = regex.firstMatch(source);
print(match?.text); // .text atribute
Regex copyWith
added.
A default implementation of copyWith for RegExp.
This method is used to create a new RegExp with the same properties of the original, but with the given values replaced.
final regex = RegExp(r'(?<name>\w+)');
final newRegex = regex.copyWith(caseSensitive: false);
Print list of matches
Use extensions to quickly print and see all matches.
You can printList
and printListWithLineBreak
.
final String source = 'Hello, my name is <name>';
final RegExp regex = RegExp(r'(?<name>\w+)');
final Iterable<RegExpMatch> matchs = regex.allMatches(source);
matchs.printListWithLineBreak();
matchs.printList();