|
|
例子:颜色:粉色[10] 尺码:S[5]
" s J" x$ U" Q8 g3 L去掉[ ]及内容:! o: [: v0 Z$ v
, @2 I0 [: K+ E- O' m
preg_replace("/\[.*\]/", '', $str)10 u) @/ R* L: Q* e, F( v
处理后效果:颜色:粉色 尺码:S
3 r4 t& J2 e# |0 c小技巧:可把[ ]改为其他符号应用在需要的地方7 f0 i( P, S) p! Z1 ^3 G
ps:下面看下利用正则表达式提取括号内内容
3 W0 \4 F: x6 o% e; Y9 O- h比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”3 j: f. K! W8 n9 r1 f ^
[code]import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test{ public static void main(String[] args) { String str ="中华人民共和国,简称(中国)。"; Matcher mat = Pattern.compile("(? |
|