tags:

views:

125

answers:

7

Regular expression for something+something+something ... +something?

Sorry because of my great english and brain -> keyboard connection

+2  A: 

If the "something" is always a word and needs at least two entries

\w(\+\w)+

or for any repeating sequence

\w(\+\w)*
Paul Alexander
+11  A: 
([a-zA-Z]+(\+[a-zA-Z]+)*)
Sean Bright
i'm still not sure if this is what the OP really wanted, but I've gotten a good laugh out of this nevertheless :)
patjbs
A: 

You just have to escape the '+' and the '.' as they are reserved.

For example, in ruby:

/something\+something\+something \.\.\. \+something/
jonnii
A: 

You can escape the + metacharacter using \+ in your regular expression, if that's what you're asking.

LBushkin
+1  A: 

Based on the comment clarifying the question, here's an updated answer:

([a-z]+\+)*[a-z]+

or

[a-z]+(\+[a-z]+)*
iammichael
A: 

As an alternative, would a string split method work?

"Or+Something+like+that".split('+')
Gavin Miller
+1 for not forcing regex to do everything.
Soviut
except that in java, String#split is a regex operation...
Tetsujin no Oni
A: 
/\w+(\s*\w+)*(\+\w+(\s*\w+)*)*/

This should also match phrases of multiple words, if you need that. It also matches non-alpha characters, but you can replace \w with [a-z] if you explicitly want to exclude everything but letters. Just make sure you specify the regex to be case insensitive if you expect capital letters.

edit: commenter below noticed I didn't have the plus sign after the \w 's, so it was only matching a single character at a time. \w+ will match entire words, with an optional space in between, which was the original intention. Thanks for the catch.

Doug R
you have the lowest reputation of the people present so
Ante B.
Well, I'm not too worried about my rep. You should pick the answer that best meets the needs of your problem, but if that's me then great.
Doug R
I'm not sure what Doug is attempting to do here? Replacing \w(\s*\w)* with \w+( \w+)* would be a better way to match phrases, if that is desired. Otherwise the whole expression should just be \w+(\+\w+)*
Peter Boughton