|
例子:颜色:粉色[10] 尺码:S[5]
$ R3 t# t9 S; s$ _去掉[ ]及内容:
; ^$ V/ E# |+ z9 @6 z# V+ Y D' C/ z* J
preg_replace("/\[.*\]/", '', $str)1% N5 ^( G9 l; a1 m" H% `" M
处理后效果:颜色:粉色 尺码:S, h6 j$ \* l0 `3 w5 M( E3 t
小技巧:可把[ ]改为其他符号应用在需要的地方
# n, T9 B! Y; b# [& Pps:下面看下利用正则表达式提取括号内内容 9 R1 c+ }1 A4 v3 [, J
比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”
3 W J& e2 V; f8 }[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("(? |
|