我想知道一个字符串是否以数字结尾(带/不带小数)。如果它结束,我想提取它。
"Test1" => 1
"Test" => NOT FOUND
"Test123" => 123
"Test1.1" => 1.1
我错过了一些细节。
1. 在数字之前,字符串也可以包含特殊字符
2. 它是单行,而不是多行。
匹配行开头、之后的任何字符和字符串末尾的数字(带有可选的小数部分)(允许尾随空格字符)。第一部分是惰性匹配,即它将匹配可能的最少字符数,将整数留给表达式的最后一部分。
^.*?(\d+(?:[.,]\d+)?)\s*$
我的测试用例
"Test1
"Test
"Test123
"Test1.1
test 1.2 times 1 is 1.2
test 1.2 times 1 is ?
test 1.2 times 1 is 134.2234
1.2
在 c# 中使用以下正则表达式(\d+)$
A regex for a string that ends with a number: @"\d$"
. Use http://regexpal.com/ to try out regexes.
Of course, that just tells you that the last character is a number. It doesn't capture anything other than the last character. To capture the number only this is needed: @"\d*\.?\d+$"
.
If your string can be more complicated, eg "Test1.2 Test2", and you want both numbers: @"\d*\.?\d+\b"
.
use this regex [a-zA-Z]+\d+([,.]\d+)?\b$
if you want digit only use this one (?<=[a-zA-Z]+)\d+([,.]\d+)?\b$