tags:

views:

101

answers:

6

Regular Expression for matching string like:

"<any_string>+<any_string>+<any_string>"?
+2  A: 

It sounds as simple as:

.*\+.*\+.*

the .* matches any_string until the \+ matches a "+" symbol.

Wallacoloo
Or maybe '[^+]*\+[^+]*\+[^+]*'...but yes...
Jonathan Leffler
or adding a not-greedy: .*?\+
seanmonstar
Those are all valid suggestions. From the OP's followup comment, it looks like this regex doesn't actually do what he wants (but it does do what he described). I'll edit it once he gives all the needed information, and include your suggestions.
Wallacoloo
A: 

Is this what you are after?

[\w\\+]*
Fadrian Sudaman
That matches an empty string and it matches '+', but it doesn't match ' + + '. It's broken.
recursive
For some reason the \\ doesnt come out with the edit, i have to edit and use double slash. It shoulds work
Fadrian Sudaman
that won't match any *string*, just any *word characters* (Though maybe that's what the OP actually wants)
Wallacoloo
Yeah if string is what he is after, then use . otherwise if only word character then use \w
Fadrian Sudaman
A: 

maybe try this

/.*\+.*\+.*/
A: 

If any_string would not contain the delimiter character >, you can easily do that using:

<([^>]+)>\+<([^>]+)>\+<([^>]+)>

$1, $2 and $3 in the match would contain the 1st, 2nd and the 3rd strings respectively. This regex doesn't allow empty strings - change ([^>]+) to ([^>]*) to allow empty strings between < and >.

Amarghosh
+1  A: 

here's a non-regex way. In your favourite language, split on "+", check the length of array to be 3. pseudocode:

s = split(mystring,"+")
if length(s) = 3 then
  .....
end if 

To be more "accurate", split on ">+<"

ghostdog74
It's not clear what the OP wants. This approach matches `<i++>=<i-->` for example.
polygenelubricants
well, then split on `>+<`
ghostdog74
more efficient is a simple `if mystring.count("+") == 2 then ...` (or ">+<")
Wallacoloo
+1  A: 

This matches S+S+S where all three occurrences of S is the same string, which is < and > surrounding any (possibly empty) string:

(<[^>]*>)\+\1\+\1

In other words, this matches:

<a>+<a>+<a>

But it doesn't match

<a>+<b>+<a>

S must be surrounded by < and >; it can contain +. So this matches:

<a+b>+<a+b>+<a+b>
polygenelubricants