Feat: Practical Examples of Regex Utility #211
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🚀 Add Practical Regex Utility Examples for Cairo Development
🛠️ Issue
📖 Description
This PR introduces a comprehensive regex utility examples project that demonstrates practical usage of regular expressions in Cairo. The implementation provides working examples of pattern matching, text searching, replacement operations, and various regex features like wildcards, character classes, and quantifiers.
Technical Implementation Details
🔧 Regex Engine Compatibility
The implementation is designed to work with the simplified Cairo regex engine that supports:
[a-z]
,[0-9]
with range notation.
for any character?
,+
,*
🚫 Limitations Addressed
{4}
syntax with explicit repetition[0-9][0-9][0-9][0-9]
📋 Configuration (
Scarb.toml
)🖼️ Screenshots
cargo test
📝 Additional Notes
📁 Project Structure
Created a new utility examples project at regex_utils with the following structure:
🎯 Core Features Implemented
1. Email Pattern Matching (
src/main.cairo
)[a-z]+@[a-z]+.[a-z]+
2. Text Search Operations
3. Text Replacement & Masking
1234-5678-9012-3456
→XXXX-XXXX-XXXX-XXXX
4. Character Classes
[0-9]+
for number sequences[a-z]+
for alphabetic text5. Wildcard Patterns
c.t
matchescat
,cut
,cot
,cit
6. Quantifiers
?
):colou?r
matches bothcolor
andcolour
+
):a+
matchesa
,aa
,aaa
*
): Pattern repetition handling🧪 Test Suite (
src/tests/test_main.cairo
)Comprehensive test coverage with 7 test functions:
test_new_and_matches
- Basic regex creation and pattern matchingtest_find
- First occurrence finding with position validationtest_find_all
- Multiple pattern matching and extractiontest_replace
- Text replacement and masking operationstest_character_classes
- Character class functionalitytest_wildcards
- Wildcard pattern matchingtest_quantifiers
- Quantifier behavior validationBuilding and Running
🔨 Build the Project
cd examples/cairo/scripts/utility_examples/regex_utils scarb build
🧪 Run the Test Suite
# Execute the main examples scarb cairo-run --package regex_utils
Example Usage
Basic Pattern Matching
Text Search and Extraction
Pattern Replacement
Benefits for Developers
Future Enhancements
This implementation provides a solid foundation for regex operations in Cairo and serves as both educational material and practical utility for developers working with pattern matching in the ecosystem.