java - Can regular expression match this pattern? -


i want match [#ababab]blah blah blah[/#ababab]

but don't want match [#ababab]blah blah blah[/#000000]

the ababab , 000000 hex color code.

the first 1 want color code of open tag , close tag same, second 1 not.

i have tried pattern did not work: \[#[0-9|a-f]{6}\].*\[/#[0-9|a-f]{6}\]

in case input can

"xxxx[#aaaaaa]yyyyy[#bbbbbb]zzzzzz[/#bbbbbb]yyyyy[/#aaaaaa]zzzzzz" 

where x have default color, y have aaaaaa color, , z have bbbbbb color, , want find

[#aaaaaa]yyyyy[#bbbbbb]zzzzzz[/#bbbbbb]yyyyy[/#aaaaaa] 

and

[#bbbbbb]zzzzzz[/#bbbbbb] 

you should add look-ahead (?=...) mechanism arshajii's answer making regex

"(?=(\\[(#[0-9a-fa-f]{6})\\].*?\\[/\\2\\]))" 

demo

string data="xxxx[#aaaaaa]yyyyy[#bbbbbb]zzzzzz[/#bbbbbb]yyyyy[/#aaaaaa]zzzzzz";  pattern p=pattern.compile("(?=(\\[(#[0-9a-fa-f]{6})\\].*?\\[/\\2\\]))"); matcher m=p.matcher(data); while(m.find())     system.out.println(m.group(1)); 

output

[#aaaaaa]xxxx[#bbbbbb]yyyy[/#bbbbbb]xxxxx[/#aaaaaa] [#bbbbbb]yyyy[/#bbbbbb] 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -