Advanced Programming-Week8

Advanced Programming-Week8

Programming 课程第八周的课堂练习

本周主要练习使用正则表达式进行匹配。

Summary

  1. final 关键字
  2. Pattern 类
    1. compile() 方法
    2. matcher() 方法
    3. CASE_INSENSITIVE 常量
  3. Matcher 类
    1. find() 方法
    2. group() 方法
    3. groupCount() 方法

Practice 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice1 {
    public static void main(String []args)
    {
        final String regex="s";
        final String string="First Semester Students";

        final Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        final Matcher matcher=pattern.matcher(string);

        while(matcher.find())
        {
            System.out.println("Full match:"+matcher.group());

            for(int i=1;i<=matcher.groupCount();i++)
            {
                System.out.println("Group" + i +":"+matcher.group(i));
            }
        }
    }
}

Practice 2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice2 {
    public static void main(String []args)
    {
        final String regex="s.";
        final String string="First Semester Students";

        final Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        final Matcher matcher=pattern.matcher(string);

        while(matcher.find())
        {
            System.out.println("Full match:"+matcher.group(0));

            for(int i=1;i<=matcher.groupCount();i++){
                System.out.println("Group"+i+":"+matcher.group(i));
            }
        }
    }
}

Practice 3

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice3 {
    public static void main(String []args)
    {
        final String regex="a{3,6}";
        final String string="a aa aaa aaaa aaaaaaaaaa";

        final Pattern pattern=Pattern.compile(regex);
        final Matcher matcher=pattern.matcher(string);

        while(matcher.find())
        {
            System.out.println("Full match:"+matcher.group(0));

            for(int i=1;i<matcher.groupCount();i++)
            {
                System.out.print("Group"+i+":"+matcher.group(i));
            }
        }
    }
}

Practice 4

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice4 {
    public static void main(String []args)
    {
        final String regex="([a-z]{1,10})";
        final String string="abc123DEF this is for lab practice";

        final Pattern pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        final Matcher matcher=pattern.matcher(string);

        while(matcher.find())
        {
            System.out.println("Full match:"+matcher.group(0));

            for(int i=0;i<matcher.groupCount();i++)
            {
                System.out.println("Group "+i+" : "+matcher.group(i));
            }
        }
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇