|
|
例子:颜色:粉色[10] 尺码:S[5]. o& u4 U. L) E& V5 Y5 O( Z
去掉[ ]及内容:( v u' z! b7 f+ ^
) A, ]( ^$ k: u! P/ i0 V0 s2 ?" Rpreg_replace("/\[.*\]/", '', $str)1* x8 O t) E; t% H+ f5 O1 g
处理后效果:颜色:粉色 尺码:S
( W, l7 Q7 Y! t& a+ C* E小技巧:可把[ ]改为其他符号应用在需要的地方9 {' S* m" O) {. {& D' ]1 m1 o* O
ps:下面看下利用正则表达式提取括号内内容
1 J7 N& P7 \0 S6 t; |比如现在要提取 中华人们共和国,简称(中国) 这句话中括号里的“中国”# d# j$ k1 T# ^' e4 B5 y
[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("(? |
|