I am attempting to parse strings to find a variable length number within a string.
Here are some strings I'm testing against...
this_is_a_test_string25365with_a_number
this_is_another_test243with_a_number
testing_again3with_a_number
$Regex = "\b.*([\d]{1,}).*\b"
foreach($result in $testdata) {
if( $result -match $Regex ) {
$Matches[1]
}
}
When I change the quantifier behind the \\d it only gives me that number of digits
If you are trying to isolate just the number, you don't need the word boundaries with \\b
and the wildcards .*
. You want just your numbers to match.
You want to use [\\d]+
which says, any digit with 1 or more in a row.
Based on your examples, look here for validation. for validation
However: if you want to capture the entire string, you can leave the word bounds and the wildcards on both ends and it will capture the full string. Example here
That .* is going to be "greedy" and suck up everything up to the minimum number of digits needed to satisfy the match.
One way to fix that:
$Regex = "\b\D*([\d]{1,}).*\b"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.