|
例子:颜色:粉色[10] 尺码:S[5]( \; e" a6 p# G2 p0 N# e& h1 n- J
去掉[ ]及内容:) K" N: V9 f4 f$ A
5 z/ {0 T- a% A
preg_replace("/\[.*\]/", '', $str)1
) b& T1 _, p p8 J. t处理后效果:颜色:粉色 尺码:S
5 T6 ~5 a% z$ ?小技巧:可把[ ]改为其他符号应用在需要的地方) L( Y% p Z/ N% j6 _
ps:下面看下利用正则表达式提取括号内内容 + B3 o1 D/ {! v0 [/ _1 `; Z8 K
比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”
5 S' w" `/ d: @7 E6 n" B# H[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("(? |
|