tags:

views:

48

answers:

2

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
12 3 45678
1234 5678
12 34567 8
1 2 3 4 5 6 7 8

At the moment I have \d{8,} but this will only capture a solid block of 8 or more digits.
[\d\s]{8,} will not work as I don't want white space to contribute to the count of chars captured.

A: 
(\d{8,}\s+)*\d{8,}

should work

leppie
Testing this in Expressio, it only matches `12345678` and none of the other examples.
Greg B
@Greg B: I see what you mean. I was not sure. To be honest, I dont think it is possible in 'vanilla' regex, but I'm sure there is a trick to handle it. Another option is to strip all the whitespace before applying the Regex.
leppie
+5  A: 
(\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,}

Amarghosh
+1: Not bad :) (filler text)
leppie
+1 `\s*\d\s*{8,}` is actually `(\s*\d\s*){8,}`
TheVillageIdiot
@TheVillateIdiot Fixed it already :)
Amarghosh