|
|
例子:颜色:粉色[10] 尺码:S[5]
' g3 u9 u2 [$ l: ^3 |9 T去掉[ ]及内容:
$ J3 Z- Y, S* c# R. H
4 k6 Q0 l0 M7 p2 K/ tpreg_replace("/\[.*\]/", '', $str)1
. u3 x0 C" s/ [8 V/ M: Y- ]( Z处理后效果:颜色:粉色 尺码:S
) k* D+ Q1 G$ Z8 k [9 f7 v小技巧:可把[ ]改为其他符号应用在需要的地方) t- e; }; b' m% l% e4 @
ps:下面看下利用正则表达式提取括号内内容 " P. P# }; m2 P( Y% p. j
比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”
+ n, w N; B* V0 `3 `[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("(? |
|