|
例子:颜色:粉色[10] 尺码:S[5]
9 U0 Z) t" h1 F. A" r去掉[ ]及内容:
$ }( U& P) l8 z: @( t6 R1 ^. v* {+ v$ Y' T6 G4 |- y
preg_replace("/\[.*\]/", '', $str)1
* M- t8 O. ~9 {5 \- T处理后效果:颜色:粉色 尺码:S$ T- K/ A* S5 ]8 ~( n
小技巧:可把[ ]改为其他符号应用在需要的地方4 N0 Y8 M I8 {, `+ b. A* D7 P( e
ps:下面看下利用正则表达式提取括号内内容
@3 v, s! O& f1 |6 S2 V比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”8 E5 E2 }$ R5 L8 _ a" \; ~0 l
[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("(? |
|