views:

47

answers:

3

Hello,

I want to be able to take a string of text from the user that should be formated like this:

.ext1 .ext2 .ext3 ...

Basically, I am looking for a dot, a string of alphanumeric characters of any length a space, and rinse and repeat. I am a little confused on how to say " i need a period, string of characters and a space". But also, the last extension could either be followed by nothing, or a space, or a series of spaces. Also, I guess in between extensions could be followed by any number of spaces?

EDIT: I made it clearer what I was looking for.

Thanks!

+1  A: 

Try this:

^(?:\.[A-Za-z0-9]+ +)*\.[A-Za-z0-9]+ *$

(Rubular)

In a Java string literal you need to escape the backslashes:

"^(?:\\.[A-Za-z0-9]+ +)*\\.[A-Za-z0-9]+ *$"
Mark Byers
What did you mean by " that there is also at least one space before that string?"
NewImageUser
I made my question clearer above
NewImageUser
Thanks a lot that did it!
NewImageUser
How can I use this to see if the string I have is a filename.extension filename.extension etc. instead of just .extension .extension etc? I'm assuming its just a simple change to allow any alphanumberic characters infront of the period instead of not any.
Diego
+1  A: 

(\.\w+)\s* Match this and get your results.
^((\.\w+)\s*)*$ Check this and if it's true, your String is exactly what you want.

For the last pattern thing, you can't (AFAIK) do both getting all extensions (separated) and checking that the last is followed by other things. Either you check your string, or you extract the extensions from it.

Colin Hebert
Basically, I just want to be able to find .txt .docSome .rtf .etasd
NewImageUser
I made my question clearer above
NewImageUser
Still, do you want to extract those extensions ? Or check that the text contains only extensions, spaces plus the last thing ?
Colin Hebert
I want to check that the string contains only extensions with dots seperated by a space. There is no "last thing"
NewImageUser
Then use my second regex.
Colin Hebert
A: 

I'd start with something like: ^.[a-z0-9]+([\t\n\v ]+.[a-z0-9]+)*$

Jerry Coffin