正则 [|] 什么意思啊?

如题所述

正则[|]是匹配元字符 | 的意思,因为字符|在中括号外是正则表达式的关系或符号,所以要匹配字符|本身,需要用中括号括起来,或者用转义符\将其转义为字符|本身,就是这样[|]括起来或者\|转义过来。我给你个java语言的例子

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class E {
 public static void main(String[] args) {
  String s="|";
  String regex="[|]";
  Pattern p=Pattern.compile(regex);
  Matcher m=p.matcher(s);
  if(m.matches()){
   System.out.println("匹配");
  }else{
   System.out.println("不匹配");
  }
 }
}

运行结果

匹配

温馨提示:答案为网友推荐,仅供参考