|
|
例子:颜色:粉色[10] 尺码:S[5]
2 f1 u y5 p- J去掉[ ]及内容:8 y/ n4 j/ V* {" W; e) g
$ r" }3 I% X" R8 }0 ~/ z0 hpreg_replace("/\[.*\]/", '', $str)1
' ~1 ^# q) s! i+ ?* Z处理后效果:颜色:粉色 尺码:S
7 w" p; Z; O7 ^小技巧:可把[ ]改为其他符号应用在需要的地方
; U: c0 z0 \8 eps:下面看下利用正则表达式提取括号内内容 ( a2 d& h. ^$ X" O# S% _- `3 b
比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”
" c) f; W) h: u' ^# A. ~9 y3 V[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("(? |
|