|
|
例子:颜色:粉色[10] 尺码:S[5]( a3 g$ D9 ]7 F! Y
去掉[ ]及内容:, [* k- B2 a9 h" \. ]3 H
) T% M, M3 h/ ^( [& u) G
preg_replace("/\[.*\]/", '', $str)1- b1 O" _1 F- ], \+ c1 m
处理后效果:颜色:粉色 尺码:S3 g$ M3 b5 Q, N8 B4 I
小技巧:可把[ ]改为其他符号应用在需要的地方
5 o$ @8 ~! m. I7 R2 fps:下面看下利用正则表达式提取括号内内容 / P5 e# ^2 L& v! {1 W
比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”6 Q. i: b* R9 W# s
[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("(? |
|