tags:

views:

78

answers:

5

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.

A: 

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

^(\s{6}[\w\s]{1,31})$
polyglot
+1  A: 

You can use a $ to indicate the end of a line, using \s* to allow optional whitespace at the end.

^\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?

bdukes
Sorry - some of the spaces seem to have got lost when I pasted the sample in - you're right.
Chris W
A: 

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

Brian Agnew
+1  A: 

Use $ to match end of line:

^(\s{6}[\w\s]{1,31})$

Or, if you may still have spaces afterwards that you want to ignore:

^(\s{6}[\w\s]{1,31})\s*$
iammichael
The later version is giving me what I need - annoyingly it doesn't work in the online test tool I've been trying to do it in but works fine in the tool Jeff Wain suggested. It could be I've had the answer earlier today and I was just using a bad test tool!
Chris W
Check your test samples are any good Chris, based on my understanding of your goal, *NEITHER* of those options will give you what you want. ' 'x6 . 'a a a' # should fail but wont fail his test
Kent Fredric
A: 

Try this one out:

^\s{6}[\w\s]{1,31}\W.*$
wergeld