This is documented behaviour for
mawk:A string expression is converted to numeric using its longest numeric prefix as with atof(3).
atofis essentially the same asstrtod, andstrtodrecognisesinforinfinity. Since both convert the longest numeric prefix,INFObecomes positive infinity.However the version of
mawkI have (1.3.4.20250131-1 from Debian 13) doesn’t match the third line — the behaviour changed in late 2024:modify calls on stdtod() to disallow hexadecimal, infinite and not-a-number values unless the "--posix" option is given
If you want to limit values to strings only containing digits, then as far as I know you’d have to match them as in your example (
$1 ~ /^[0-9]+$/) since infinity and NaN are allowed (and even “0x”-prefixed hexadecimal numbers).
awk - The string "INFO" matches mawk's inf (infinity) - Unix & Linux Stack Exchange
What's The True Difference Between string.find(), string.match(), [string].find(), [string].match(), [string]:find(), [string]:match()?
Regex string match?
How to match a String against string literals? - Stack Overflow
Videos
If you want the literal strings abc or def followed by 8-11 digits, you need something like:
(abc|def)[0-9]{8,11}
You can test it here: http://www.regular-expressions.info/javascriptexample.html
Be aware that, if you don't want to match more than 11 digits, you will require an anchor (or [^0-9]) at the end of the string. If it's just 8 or more, you can replace {8,11} with {8}.
To elaborate on an already posted answer, you need a global match, as follows:
var matches = string.match(/(abc|def)\d{8,11}/g);
This will match all subsets of the string which:
- Start with "abc" or "def". This is the "(abc|def)" portion
- Are then followed by 8-11 digits. This is the "\d{8,11}" portion. \d matches digits.
The "g" flag (global) gets you a list of all matches, rather than just the first one.
In your question, you asked for 8-11 characters rather than digits. If it doesn't matter whether they are digits or other characters, you can use "." instead of "\d".
I also notice that each of your example matches have more than 11 characters following the "abc" or "def". If any number of digits will do, then the following regex's may be better suited:
- Any number of digits -
var matches = string.match(/(abc|def)\d*/g); - At least one digit -
var matches = string.match(/(abc|def)\d+/g); - At least 8 digits -
var matches = string.match(/(abc|def)\d{8,}/g);
UPDATE:
Use .as_str() like this to convert the String to an &str:
Copymatch stringthing.as_str() {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
_ => println!("something else!"),
}
Reason
.as_str() is more concise and enforces stricter type checking. The trait as_ref is implemented for multiple types and its behaviour could be changed for type String, leading to unexpected results. Similarly, if the input argument changes type, the compiler will not signal a problem when that type implements the trait as_ref.
The docs suggest to use as_str as well https://doc.rust-lang.org/std/string/struct.String.html, https://doc.rust-lang.org/std/primitive.str.html
Old answer:
as_slice is deprecated, you should now use the trait std::convert::AsRef instead:
Copymatch stringthing.as_ref() {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
_ => println!("something else!"),
}
Note that you also have to explicitly handle the catch-all case.
You can do something like this:
Copymatch &stringthing[..] {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
_ => println!("something else!"),
}
There's also an as_str method as of Rust 1.7.0:
Copymatch stringthing.as_str() {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
_ => println!("something else!"),
}