Alphanumeric
This library provides character filtering, numeric validation, and international number extraction for Rust strings. It builds on the Rust standard library with no regex dependency. It focuses on stripping or filtering characters by type and parsing numbers from free text, with full support for international decimal and thousands separator conventions (points, commas, spaces, apostrophes, middle dots).
Dependencies
- to-segments — Splits string slices and owned strings into vectors and extracts optional strings, pairs of optional strings.
Features
| Feature | Default | Description |
|---|---|---|
cell_analysis |
no | Column-level decimal separator detection via analyze_cell, detect_column_format, and CellAnalysis |
Enable in Cargo.toml:
[]
= { = "0.1", = ["cell_analysis"] }
Related crates
- to-segments — Provides the
ToSegmentstrait for ergonomic string splitting with readable methods for common manipulation tasks. Also used internally byalphanumeric. - enclose-strings — Wrap or enclose strings in matching or complementary characters with optional escaping.
- simple-string-patterns — Provides simple methods to match and filter strings by simple patterns without regular expressions.
Method overview
| Componentposition | Meaning |
|---|---|
| strip_by_ ⇤ | Return a string without the specified character type(s) |
| filter_by_ ⇤ | Return a string with only the specified character type(s) |
| to_numbers ⇤ | Extract and parse numeric values from free text |
| to_numeric_strings ⇤ | Extract numeric substrings without parsing |
| _strict ⇥ | Variant accepting an explicit DecimalSeparator argument |
Examples
Check if a string is a valid number
use IsNumeric;
let num_str = "-1227.75";
assert!; // true
let num_str = "12a34";
assert!; // false — contains non-numeric character
Extract the first decimal value from a longer string
use StripCharacters;
const GBP_TO_EURO: f64 = 0.835;
let sample_str = "Price £12.50 each";
if let Some = sample_str.
Extract numeric sequences from phrases and convert them to a vector of floats
use ;
// Extract European-style numbers with commas as decimal separators
// and dots as thousand separators
let sample_str = "2.500 grammi di farina costa 9,90€ al supermercato.";
let numbers: = sample_str.to_numbers_strict;
if numbers.len > 1
Split a string list of numbers into floats
use StripCharacters;
// Extract 64-bit floats from a comma-separated list.
// Numbers within each segment are evaluated separately.
let sample_str = "34.2929,-93.701";
let numbers = sample_str.;
// yields vec![34.2929, -93.701]
Extract all numbers from a sentence
use StripCharacters;
let source_str = "I spent £9999.99 on 2 motorbikes at the age of 72.";
assert_eq!;
assert_eq!;
assert_eq!;
International number formats
The DecimalSeparator enum controls how ambiguous separators are interpreted:
use ;
// US/UK format: commas as thousands, dot as decimal
let us_text = "Price: $1,234.56";
let nums: = us_text.to_numbers;
assert_eq!;
// European format: dots as thousands, comma as decimal
let eu_text = "Kaufpreis: 1.500,50 EUR";
let nums: = eu_text.to_numbers_strict;
assert_eq!;
// French format: spaces as thousands, comma as decimal
let fr_text = "Le prix est 19 999,99 euros";
let nums: = fr_text.to_numbers;
assert_eq!;
// Swiss format: apostrophes as thousands
let ch_text = "The cost is CHF 19'999.99";
let nums: = ch_text.to_numbers;
assert_eq!;
Normalize numeric strings
use StripCharacters;
// Correct international separators to standard dot-decimal format
let european = "1.999.999,25";
assert_eq!;
let english = "1,999,999.25";
assert_eq!;
Filter strings by character categories
use ;
let sample_str = "Products: $9.99 per unit, £19.50 each, €15 only. Zürich café cañon";
let vowels_only = sample_str.filter_by_type;
// yields "oueuieaoyüiaéao"
let lower_a_to_m = sample_str.filter_by_type;
// yields "dceieachlichcafca"
// Filter by multiple character categories
let lower_and_spaces = sample_str.filter_by_types;
// yields "roducts per unit each only ürich café cañon"
Strip spaces
use StripCharacters;
let sample_str = "19 May 2021 ";
let without_spaces = sample_str.strip_spaces;
// yields "19May2021"
Remove character categories from strings
use ;
let sample_str = "Products: $9.99 per unit, £19.50 each, €15 only. Zürich café cañon";
let without_punctuation = sample_str.strip_by_type;
// yields "Products 999 per unit £1950 each €15 only Zürich café cañon"
let without_spaces_and_punct = sample_str.strip_by_types;
// yields "Products999perunit£1950each€15onlyZürichcafécañon"
Validate strings with character classes
use CharGroupMatch;
assert!; // true — contains digit characters
assert!; // false
assert!; // true
assert!; // true
assert!; // true
assert!; // false
// Hexadecimal digit validation
assert!; // true
Detect decimal separator format across a column of data
Requires the cell_analysis feature.
use ;
// Analyze individual cells
assert_eq!;
assert_eq!;
assert_eq!;
// Detect format across a column of values
let cells = vec!;
assert_eq!;
Traits
| Name | No. of methods | Description |
|---|---|---|
| IsNumeric | 1 | Check if a string can be parsed to an integer or float |
| StripCharacters | 17 | Strip unwanted characters by type, or extract vectors of numeric strings, integers or floats |
| CharGroupMatch | 6 | Validate strings with character classes: has_digits, has_alphanumeric, has_alphabetic, is_digits_only |
IsNumeric
Strict check on a numeric string before using .parse::<T>(). Returns true for integers, negative numbers, and decimals with a single . separator.
use IsNumeric;
assert!;
assert!;
assert!;
assert!; // multiple decimal points
assert!; // empty string
StripCharacters
Strip unwanted characters by type or extract vectors of numeric strings, integers or floats without regular expressions.
| Method | Description |
|---|---|
strip_non_alphanum() |
Remove all non-alphanumeric characters |
strip_non_digits() |
Remove all non-digit characters |
strip_spaces() |
Remove whitespace characters |
strip_by_type(ct) |
Remove characters matching a specific CharType |
strip_by_types(cts) |
Remove characters matching any of the specified CharTypes |
filter_by_type(ct) |
Keep only characters matching a specific CharType |
filter_by_types(cts) |
Keep only characters matching any of the specified CharTypes |
to_numeric_strings() |
Extract numeric substrings using auto-detection |
to_numeric_strings_strict(sep) |
Extract numeric substrings with explicit DecimalSeparator |
to_numbers::<T>() |
Parse extracted numbers using auto-detection |
to_numbers_strict::<T>(sep) |
Parse extracted numbers with explicit DecimalSeparator |
to_first_number::<T>() |
Extract the first parsed number, or None |
to_first_number_strict::<T>(sep) |
Extract the first parsed number with explicit separator |
split_to_numbers::<T>(pattern) |
Split by pattern and extract the first number from each segment |
strip_non_numeric() |
Extract numeric strings and join them with spaces |
correct_numeric_string() |
Normalize separators to standard dot-decimal format |
correct_numeric_string_strict(sep) |
Normalize separators with explicit DecimalSeparator |
CharGroupMatch
Validate strings with character classes.
| Method | Description |
|---|---|
has_digits() |
Contains at least one ASCII digit |
has_digits_radix(radix) |
Contains at least one digit in the given radix |
has_alphanumeric() |
Contains at least one alphanumeric character |
has_alphabetic() |
Contains at least one alphabetic character |
is_digits_only() |
All characters are ASCII digits |
is_digits_only_radix(radix) |
All characters are digits in the given radix |
Enums
DecimalSeparator
Controls how ambiguous separators (dots, commas) are interpreted when extracting numbers.
| Variant | Meaning |
|---|---|
Point |
Treat . as the decimal separator (US, UK, Asia, etc.) |
Comma |
Treat , as the decimal separator (many European, Latin American, African countries) |
Auto |
Intelligently detect based on separator patterns (recommended for mixed contexts) |
CharType
Defines categories, sets or ranges of characters as well as single characters. Used with strip_by_type, strip_by_types, filter_by_type, and filter_by_types.
| Variant | Arguments | Meaning |
|---|---|---|
Any |
— | Matches any character |
DecDigit |
— | Match 0–9 only (is_ascii_digit) |
Digit |
(u32) |
Match digit with the specified radix (e.g. 16 for hexadecimal) |
Numeric |
— | Match number-like characters in the decimal base (excludes . and -) |
AlphaNum |
— | Match any alphanumeric characters (is_alphanumeric) |
Lower |
— | Match lower case letters (is_lowercase) |
Upper |
— | Match upper case letters (is_uppercase) |
Alpha |
— | Match any letters in most supported alphabets (is_alphabetic) |
Spaces |
— | Match whitespace (is_whitespace) |
Punctuation |
— | Match ASCII punctuation (is_ascii_punctuation) |
Char |
(char) |
Match a single character |
Chars |
(&[char]) |
Match an array of characters |
Range |
(Range<char>) |
Match a range, e.g. 'a'..'d' includes a, b, c but not d |
Between |
(char, char) |
Match characters between the specified bounds, inclusive on both ends |
CellAnalysis (feature: cell_analysis)
Result of analyzing a single cell's decimal separator format, returned by analyze_cell().
| Variant | Meaning |
|---|---|
Point |
Clearly uses . as decimal separator |
Comma |
Clearly uses , as decimal separator |
Either |
Ambiguous — could use either format |
None |
No decimal separator present |
Functions (feature: cell_analysis)
analyze_cell
analyze_cell(txt: &str) -> CellAnalysis
Analyze a single numeric string to determine which decimal separator convention it uses. Examines the positions of dots, commas, spaces, apostrophes and middle dots to make a determination.
detect_column_format
detect_column_format(cells: &[&str], max_scan: usize) -> DecimalSeparator
Scan a column of cell values and detect the predominant decimal separator format. Stops early when the format becomes clear (3+ votes with a 2+ lead). Useful for batch processing tabular data where the format is consistent within a column.
uses_decimal_comma
uses_decimal_comma(txt: &str, enforce_euro_mode: bool) -> bool
Detect if a numeric string uses European format with , as the decimal separator and dots as thousand separators. When enforce_euro_mode is true, a single comma is always treated as a decimal separator; otherwise it may be interpreted as a thousands separator depending on position.