regular expression to match six spaces followed by up to 31 alphanumerics

regular expression to match six spaces followed by up to 31 alphanumerics

It's getting towards the end of the day and this is annoying me - one day I'll find the time to learn regex properly as I know it can save a lot of time when extracting info from text.

I need to match strings that match the following signature:

6 spaces followed by up 31 alphanumerics (or spaces) and then no more alphanumeric text on that line.


 '      sampleheading                                                  ' - is fine  '      sampleheading                                      10^21/1     ' - should not match  '      sampleheading                                      sample      ' - should not match 

I've got ^(\s{6}[\w\s]{1,31}) matching the first bit correctly I think but I can't seem to get it to only select lines that don't have any text following the initial match.

Any help appreciated!


I've updated the text as a number of you noted my hastily entered original samples would actually all have tested fine.

How do you reference unicode characters in ColdFusion regex?


does awk support quantifiers - i want [0-9]{6,6}
Use $ to match end of line:.
Regexp pattern matching IP and UserAgent in an Huge File
Or, if you may still have spaces afterwards that you want to ignore:.
Regex to insert newlines into large block of text at specific locations

What is the most correct regular expression for a UNIX file path?

Emacs - regular expressions in Lisp need to be double-escaped - why?


Looking for a URI match on this regular expression, but it is crashing regex testers
You can use a $ to indicate the end of a line, using \s* to allow optional whitespace at the end..
Looking for some URL rewriting help
Your samples don't match what you're saying you're wanting, however.

They only start with four spaces, rather than six, and, in the last sample, "sampleheading sample" is within the 31 character limit, so it matches, too.

(The middle sample is within the length, too, but has non-word characters in it, so it doesn't match).

Is that what you want?.


add a $ to match the end of the line, e.g..


Aren't you simply saying 'match 6 spaces followed by 31 alphanumerics' ? There's not concept there of 'and no more alphanumerics' . I think what you have is good so far (!), but you need to follow it with (say) [^\w] - i.e.

'not an alphanumeric'..


Try this one out:.

93 out of 100 based on 53 user ratings 1353 reviews