Ignoring white space for a Regex match

前端 未结 3 864
独厮守ぢ
独厮守ぢ 2020-12-16 22:19

I need to match 8 or more digits, the sequence of which can include spaces.

for example, all of the below would be valid matches.

12345678
1 2345678
         


        
3条回答
  •  感情败类
    2020-12-16 22:43

    (\d *){8,}
    

    It matches eight or more occurrences of a digit followed by zero or more spaces. Change it to

    ( *\d *){8,}  #there is a space before first asterik
    

    to match strings with spaces in the beginning. Or

    (\s*\d\s*){8,}
    

    to match tabs and other white space characters (that includes newlines too).

    Finally, make it a non-capturing group with ?:. Thus it becomes (?:\s*\d\s*){8,}

提交回复
热议问题