且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式中的可选字符

更新时间:2023-01-17 08:31:40

怎么样:

^[0-9+-]*[0-9][0-9+-]*$

这确保字符串中的某处至少有一个数字. (虽然看起来可能有很多回溯.但是,另一方面,它没有在另一个+或*内包裹+或*,我也不喜欢.)

This ensures that there is at least one digit somewhere in the string. (It looks like it might have a lot of backtracking, though. But on the other hand it doesn't have a + or * wrapped inside another + or *, which I don't like either.)