加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

14-01 Java matches类,Pattern类,matcher类

发布时间:2020-12-14 06:09:03 所属栏目:Java 来源:网络整理
导读:Pattern类 正则表达式常见规则 'a' 'u000A' 'u000D' B:字符类 [abc] a、b 或 c(简单类),其中一个 [ ^span style="color: #000000;"abc] 任何字符,除了 a、b 或 c(否定) [a -zA-span style="color: #000000;"Z] a到 z 或 A到 Z,两头的字母包括在内(

Pattern类

正则表达式常见规则

'a''u000A''u000D'B:字符类
[abc] a、b 或 c(简单类),其中一个
[
^<span style="color: #000000;">abc] 任何字符,除了 a、b 或 c(否定)
[a
-zA-<span style="color: #000000;">Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[
0-9<span style="color: #000000;">] 0到9的字符都包括

C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢?<span style="color: #000000;"> .
d 数字:[0-9<span style="color: #000000;">]
w 单词字符:[a-zA-Z_0-9<span style="color: #000000;">]
在正则表达式里面组成单词的东西必须有这些东西组成

D:边界匹配器
^<span style="color: #000000;"> 行的开头
$ 行的结尾
b 单词边界
就是不是单词字符的地方。
举例:hello world?<span style="color: #000000;">haha;xixi

E:Greedy 数量词
X?<span style="color: #000000;"> X,一次或一次也没有
X*<span style="color: #000000;"> X,零次或多次
X+<span style="color: #000000;"> X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次

正则表达式的常见功能

A:判断功能
String类的public boolean matches(String regex)

String regex = "1[38]d{9}"<span style="color: #008000;">//<span style="color: #008000;">调用功能,判断即可
<span style="color: #0000ff;">boolean flag = phone.matches(regex);

// String regex = "w+@w{2,6}(.w{2,3})+"<span style="color: #008000;">//<span style="color: #008000;">调用功能,判断即可
<span style="color: #0000ff;">boolean flag = email.matches(regex);

B:分割功能
String类的public String[] split(String regex)

String ages = "18-24"/<span style="color: #000000;">定义规则
String regex
= "-"<span style="color: #000000;">;

<span style="color: #008000;">//<span style="color: #008000;">调用方法
String[] strArray = ages.split(regex);

= "aa.bb.cc" String s4 = "E:JavaSEday14avi"= s4.split("\"

把字符串分割排序

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.Arrays;

<span style="color: #008000;">/*<span style="color: #008000;">

  • 我有如下一个字符串:"91 27 46 38 50"

  • 请写代码实现最终输出结果是:"27 38 46 50 91"

  • 分析:

  • A:定义一个字符串

  • B:把字符串进行分割,得到一个字符串数组

  • C:把字符串数组变换成int数组

  • D:对int数组排序

  • E:把排序后的int数组在组装成一个字符串

  • F:输出字符串
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> RegexTest {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 定义一个字符串
    String s = "91 27 46 38 50"<span style="color: #000000;">;

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把字符串进行分割,得到一个字符串数组</span>
     String[] strArray = s.split(" "<span style="color: #000000;"&gt;);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把字符串数组变换成int数组</span>
     <span style="color: #0000ff;"&gt;int</span>[] arr = <span style="color: #0000ff;"&gt;new</span> <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt;[strArray.length];
    
     </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;int</span> x = 0; x < arr.length; x++<span style="color: #000000;"&gt;) {
         arr[x] </span>=<span style="color: #000000;"&gt; Integer.parseInt(strArray[x]);
     }
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 对int数组排序</span>

    <span style="color: #000000;"> Arrays.sort(arr);

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把排序后的int数组在组装成一个字符串</span>
     StringBuilder sb = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; StringBuilder();
     </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;int</span> x = 0; x < arr.length; x++<span style="color: #000000;"&gt;) {
         sb.append(arr[x]).append(</span>" "<span style="color: #000000;"&gt;);
     }
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;转化为字符串</span>
     String result =<span style="color: #000000;"&gt; sb.toString().trim();
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;输出字符串</span>
     System.out.println("result:"+<span style="color: #000000;"&gt;result);

    }
    }

C:替换功能
String类的public String replaceAll(String regex,String replacement)

<span style="color: #008000;">/*<span style="color: #008000;">

  • 替换功能

  • String类的public String replaceAll(String regex,String replacement)

  • 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> RegexDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 定义一个字符串
    String s = "helloqq12345worldkh622112345678java"<span style="color: #000000;">;

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 直接把数字干掉</span>
     String regex = "d+"<span style="color: #000000;"&gt;;
     String ss </span>= ""<span style="color: #000000;"&gt;;
    
     String result </span>=<span style="color: #000000;"&gt; s.replaceAll(regex,ss);
     System.out.println(result);

    }
    }

?

D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");

find():查找存不存在
group():获取刚才查找过的数据

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.regex.Matcher;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.regex.Pattern;

<span style="color: #008000;">/*<span style="color: #008000;">

  • 获取功能

  • Pattern和Matcher类的使用

  • 模式和匹配器的基本使用顺序
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> RegexDemo {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 模式和匹配器的典型调用顺序

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把正则表达式编译成模式对象</span>
     Pattern p = Pattern.compile("a*b"<span style="color: #000000;"&gt;);
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串</span>
     Matcher m = p.matcher("aaaaab"<span style="color: #000000;"&gt;);
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 调用匹配器对象的功能</span>
     <span style="color: #0000ff;"&gt;boolean</span> b =<span style="color: #000000;"&gt; m.matches();
     System.out.println(b);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;这个是判断功能,但是如果做判断,这样做就有点麻烦了,我们直接用字符串的方法做</span>
     String s = "aaaaab"<span style="color: #000000;"&gt;;
     String regex </span>= "a*b"<span style="color: #000000;"&gt;;
     </span><span style="color: #0000ff;"&gt;boolean</span> bb =<span style="color: #000000;"&gt; s.matches(regex);
     System.out.println(bb);

    }
    }

?

<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.regex.Matcher;
<span style="color: #0000ff;">import<span style="color: #000000;"> java.util.regex.Pattern;

<span style="color: #008000;">/*<span style="color: #008000;">

  • 获取功能:

  • 获取下面这个字符串中由三个字符组成的单词

  • da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
    <span style="color: #008000;">*/
    <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> RegexDemo2 {
    <span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
    <span style="color: #008000;">//<span style="color: #008000;"> 定义字符串
    String s = "da jia ting wo shuo,gao xing bu?"<span style="color: #000000;">;
    <span style="color: #008000;">//<span style="color: #008000;"> 规则
    String regex = "bw{3}b"<span style="color: #000000;">;

     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 把规则编译成模式对象</span>
     Pattern p =<span style="color: #000000;"&gt; Pattern.compile(regex);
    
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 通过模式对象得到匹配器对象</span>
     Matcher m =<span style="color: #000000;"&gt; p.matcher(s);
    
     </span><span style="color: #0000ff;"&gt;while</span><span style="color: #000000;"&gt; (<span style="color: #ff0000;"&gt;m.find()</span>) {
         System.out.println(m.group());
     }
    
     </span><span style="color: #ff0000;"&gt;// 注意:一定要先find(),然后才能group()
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; IllegalStateException: No match found
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; String ss = m.group();
     </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; System.out.println(ss);</span>

    <span style="color: #000000;">

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读