开发者

Regex help required - V2 formatted [duplicate]

开发者 https://www.devze.com 2023-01-19 10:06 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Regex help required I am trying to replace two or more occurences of <br/> (like <br/><br/
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Regex help required

I am trying to replace two or more occurences of <br/> (like <br/><br/><br/>)tags together with two <br/><br/> with the following pattern

Pattern brTagPattern = Pattern.compile("(<\\s*br\\s*/\\s*>\\s*){2,}",
    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

But there are some cases where '<br/> <br/>' tags come with a space and they ge开发者_StackOverflow社区t replaced with 4 <br/> tags which was actually supposed to be replaced with just 2 tags.

What can i do to ignore 2 or 3(few) spaces that come in between the tags ?


You can do that changing a little your regex:

Pattern brTagPattern = Pattern.compile("<\\s*br\\s*/\\s*>\\s*<\\s*br\\s*/\\s*>\\s*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

This will ignore every spaces between two
. If you just want exactly 2 or three, you can use:

Pattern brTagPattern = Pattern.compile("<\\s*br\\s*/\\s*>(\\s){2,3}<\\s*br\\s*/\\s*>\\s*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
0

精彩评论

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