|
例子:颜色:粉色[10] 尺码:S[5]* L1 d3 u( R3 y! B4 s4 y
去掉[ ]及内容:
, F* T- E- t, T. |( g( u' A2 w. h
preg_replace("/\[.*\]/", '', $str)1. C2 k: p5 Y A7 b6 m( R
处理后效果:颜色:粉色 尺码:S3 u% Z l$ M4 O# B
小技巧:可把[ ]改为其他符号应用在需要的地方
+ K) I) Y" d7 {: g9 C$ jps:下面看下利用正则表达式提取括号内内容
2 `5 |. |8 L3 m/ w W( z5 @比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”' w, Z i" k3 x# }# D2 v* 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("(? |
|