Rust Advanced String Generator
This project provides a powerful and flexible string generator based on regex-like patterns, with support for features such as character classes, custom repeats, array-based string selection, incremental values, and more. The application can be compiled and used both as a native Rust library and in a WebAssembly (WASM) context.
Demo
Demo for this project below https://igarashi.net/rust-advanced-string-generator/
Features
- Character Classes: Supports
\d
,\w
,\s
,\D
,\W
,\S
, etc. - Custom Repeats: Generate strings with patterns like
\d{2,4}
. - Character Ranges: Use ranges like
[a-z]
,[A-Z]
,[0-9]
to specify sets of characters. - Negation in Ranges: Specify characters not to be included with patterns like
[^a-z]
. - Incremental Values: Automatically increment values using patterns like
\i+
for ascending and\i-
for descending. - Array-Based Selection: Choose from an array of strings using patterns like
\a
,\a+
, and\a-
. - Group Capturing and Backreferences: Capture groups of characters and reference them later in the pattern.
- WASM Support: Compile the project to WebAssembly and use it in a web environment.
Installation
- Clone the repository:
- Navigate to the project directory:
- Build the project:
For WebAssembly
- Ensure you have
wasm-pack
installed:
- Build the project targeting WebAssembly:
- Use the generated WebAssembly module in your web project.
Usage
Examples
-
Generate a string with an incrementing value with leading zeros:
-
Generate a random string from an array:
-
Use a combination of patterns:
WASM Example
import init from './pkg/regex_generator_wasm.js';
;
Options
Option | Description |
---|---|
-h , --help |
Prints help information |
-v , --version |
Prints version information |
-p , --pattern |
Specifies the pattern to use |
-i , --increment |
Initial value for the increment (optional) |
-a , --array |
Array of strings (comma-separated) for /a pattern (optional) |
Supported Patterns
Pattern | Description | Example Input | Example Output |
---|---|---|---|
\d |
Any digit from 0 to 9 . |
\d\d |
42 , 07 |
\w |
Any "word" character: letters, digits, and underscores. | \w\w\w |
abc , 1X_ |
\s |
Any whitespace character (space, tab, newline). | \s\s |
, \t |
\D |
Any non-digit character. | \D\D |
AB , -- |
\W |
Any non-word character. | \W\W |
** , @# |
\S |
Any non-whitespace character. | \S\S\S |
abc , a1b |
{n,m} |
Insert between n and m times. |
\d{2,4} |
12 , 4321 |
[abc] |
Insert any one of the characters a , b , or c . |
[abc]{3} |
abc , cab |
[^abc] |
Insert any character except a , b , or c . |
[^abc]{3} |
xyz , 123 |
[a-z] |
Insert any character in the range from a to z . |
[a-z]{3} |
abc , xyz |
[0-9]{n:z} |
Insert any number in the range from 0 to 9 , n times with z of leading zero. |
[0-9]{3:5} |
00827 , 00281 |
\i or \i+ |
Insert an incrementing value, starting from the specified value and increasing with each use. | \i+\d\d |
1300 , 1301 |
\i- |
Insert a decrementing value, starting from the specified value and decreasing with each use. | \i-\d\d |
1299 , 1298 |
\i{:z} |
Insert a incrementing value, starting from the specified value and leading zero. | \i{:6} |
001299 , 001298 |
\a |
Insert a random string from an array of values. | \a |
apple , banana |
\a+ |
Insert a string from an array in ascending order. | \a+ |
apple , banana |
\a- |
Insert a string from an array in descending order. | \a- |
cherry , banana |
() |
Group characters. | (\d\d) |
42 |
\1 |
Backreference to the first captured group. | (\d\d)\1 |
4242 |
` | ` | Alternation; insert either the expression before or the expression after. | `a |
Testing
Native Rust
To run the tests in a native Rust environment, execute:
WebAssembly
To build and test the WebAssembly version, follow these steps:
-
Build the WASM module:
-
Create an HTML file to load and test the WASM module.
-
Serve the HTML file locally using a server like Python's
http.server
:
Help
To get help information, run:
Version
To check the version of the tool, run:
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please submit a pull request or create an issue to report bugs or suggest features.