简体   繁体   English

正则表达式匹配包含空格但不能全部为空格的数字?

[英]regex match numbers that contain spaces but cannot be all spaces?

i'm writing a python regex code to test if a string may be numbers. 我正在编写python正则表达式代码以测试字符串是否可以是数字。 i wrote follows: 我写了如下:

import regex as re
re.match('(([-\s]*)([\d\s,]+)(\.?)([\d\s]*)(%?))',' - 1, 00 0.0 0 %')

this one works. 这个作品。 but how to make this example string( '.01' or .01 ) not matched? 但是如何使此示例字符串( '.01'.01 )不匹配? ie how to make the pattern matches the string that contains any digits, , , . 即如何使图案包含任何数字,字符串匹配,. ,space and % , but not matches the one that contents before . ,space和% ,但不匹配之前的内容. are only spaces or nothing. 只是空格或一无所有。

Just add a lookahead (?=[\\s,]*\\d) before matching the digits before the . 只需在匹配.之前的数字之前添加前瞻(?=[\\s,]*\\d) . , asserting that there is a digit (before the . ): ,声称有一个数字(在.之前):

(([-\s]*)(?=[\s,]*\d)([\d\s,]+)(\.?)([\d\s]*)(%?))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM