开发者

Regular Expression for matching string like: "<any_string>+<any_string>+<any_string>"

Regular Expre开发者_运维知识库ssion for matching string like:

"<any_string>+<any_string>+<any_string>"?


It sounds as simple as:

.*\+.*\+.*

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


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>


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 ">+<"


maybe try this

/.*\+.*\+.*/


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


Is this what you are after?

[\w\\+]*
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜