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.

E.g.

 '      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!

Edit:

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?

1:



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
^(\s{6}[\w\s]{1,31})$ 
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
^(\s{6}[\w\s]{1,31})\s*$ 


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


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

2:



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
^\s{6}[\w\s]{1,31}\s*$ 
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?.


3:


add a $ to match the end of the line, e.g..
^(\s{6}[\w\s]{1,31})$ 


4:


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'..


5:


Try this one out:.
^\s{6}[\w\s]{1,31}\W.*$ 



93 out of 100 based on 53 user ratings 1353 reviews

@