. 匹配任意一个字符

* 表示匹配0个或多个前面这个字符

+ 表示1个或多个前面这个字符

? 表示0个或1个前面这个字符

^ 表示一行的开始   ^[a-zA-Z] :表示开头是a-z或者A-Z

  [^0-9] :表示不是数字,除数字以外的

$ 表示一行的结束

\w 表示词字符[a-zA-Z0-9]

\W 表示非词字符[^\w]

第七题:

  package net.mindview.strings.test7;
public class Test7 {

    public static void main(String[] args) {
//两种写法都可以
String regex = "^[A-Z].*\\.$";//"^[A-Z].*\\."这样也对
String regex1 = "\\p{Upper}.*\\.$";
String str = "D.";
String str1 = "Dfasdfasfasfdasfdasfasfasdf.";
String str2 = "Dfasdfasfasfdasfdasfasfasdf.E";
System.out.println(str.matches(regex));
System.out.println(str1.matches(regex));
System.out.println(str2.matches(regex));
System.out.println(str.matches(regex1));
System.out.println(str1.matches(regex1));
System.out.println(str2.matches(regex1));
}
}

运行结果:

  true
true
false
true
true
false

第八题

package net.mindview.strings;

import java.util.Arrays;

public class Splitting {

    public static String knights = "Then, when you have found the shrubbery, you must cut down the mightiest tree in the forest... with... a herring!";

    public static void split(String regex){
System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
//表示的时按照空格分割字符串
//运行结果:[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest..., with..., a, herring!]
split(" "); //表示按照非单次字符分割字符串--这里的非单次字符是空格和,
//运行结果:[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
split("\\W+");
//这个表示:费单次字符之前带n的地方进行分割字符串 这里的分割符是n空格和n,
//运行结果:[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest... with... a herring!]
split("n\\W+");
} }
package net.mindview.strings.test8;

import net.mindview.strings.Splitting;

public class Test8 {

    public static void main(String[] args) {
String regex = "the|you";
Splitting.split(regex);
}
}

第九题

package net.mindview.strings.test9;

import net.mindview.strings.Splitting;

public class Test9 {
public static void main(String[] args) {
String regex = "A|E|I|O|U|a|e|i|o|u";
//通过嵌入式标志表达式 (?i) 也可以启用不区分大小写的匹配。
String regex1 = "(?i)a|e|i|o|u";
//[abc] 表示a或b或c
String regex2 = "(?i)[aeiou]";
System.out.println(Splitting.knights.replaceAll(regex, "_"));
System.out.println(Splitting.knights.replaceAll(regex1, "_"));
System.out.println(Splitting.knights.replaceAll(regex2, "_"));
}
}

java编程思想-第13章-某些练习题的更多相关文章

  1. java编程思想-第六章-某些练习题

    参考https://blog.csdn.net/caroline_wendy/article/details/47271037 3 package debug; import java.util.Ar ...

  2. java编程思想-第五章-某些练习题

    参考https://blog.csdn.net/caroline_wendy/article/details/46844651 10&11 finalize()被调用的条件 Java1.6以下 ...

  3. Java编程思想 第21章 并发

    这是在2013年的笔记整理.现在重新拿出来,放在网上,重新总结下. 两种基本的线程实现方式 以及中断 package thread; /** * * @author zjf * @create_tim ...

  4. Java编程思想——第17章 容器深入研究 读书笔记(三)

    七.队列 排队,先进先出. 除并发应用外Queue只有两个实现:LinkedList,PriorityQueue.他们的差异在于排序而非性能. 一些常用方法: 继承自Collection的方法: ad ...

  5. Java编程思想——第17章 容器深入研究(two)

    六.队列 排队,先进先出.除并发应用外Queue只有两个实现:LinkedList,PriorityQueue.他们的差异在于排序而非性能. 一些常用方法: 继承自Collection的方法: add ...

  6. Java编程思想-第四章练习题

    练习1:写一个程序,打印从1到100的值 public class Print1To100{ public static void main(String args[]){ for(int i = 1 ...

  7. Java编程思想笔记(第二章)

    第二章  一切都是对象 尽管Java是基于C++的,但相比之下,Java是一种更纯粹的面向对象程序设计语言. c++和Java都是杂合型语言(hybird language) 用引用(referenc ...

  8. java编程思想笔记(第一章)

    Alan Kay 第一个定义了面向对象的语言 1.万物皆对象 2.程序是对象的集合,他们彼此通过发送消息来调用对方. 3.每个对象都拥有由其他对象所构成的存储 4.每个对象都拥有其类型(TYpe) 5 ...

  9. Java编程思想第七章复用类

    7.1组合语法 在一个类中引入多个对象,以提高代码的复用性与功能. 7.2继承语法 使用继承子类可以获得,导出类可以获得基类的成员(变量与方法). 注:这里注意权限控制,若基类中的成员为默认权限,只有 ...

随机推荐

  1. Unsupported major.minor version 51.0 错误解决方案

    jdk1.6工程中使用外部jar包中类出现:Unsupported major.minor version 51.0原因分析:出现上述错误是因为:外部jar包使用jdk1.7(jdk7)编译,而使用此 ...

  2. python常见模块之time,datetime模块

    一.time模块 time模块提供了一些用于管理时间和日期. time模块中时间的表现形式有三种: format_string  格式化的字符串 struct_time     结构化时间 times ...

  3. Python小游戏之 - 飞机大战美女 !

    用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...

  4. strtok函数读写冲突问题

    先上测试代码 #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int a ...

  5. vfd电子时钟制作

    17年也没干个啥,年后就去折腾着玩意儿了,也不知道我折腾它还是它折腾我.反正总之现在勉强可以交作业了,呵呵 硬件: 1.罗耶振荡电路输出一路4v交流,一路25v交流 其中4v直接驱动灯丝,另一路经电桥 ...

  6. Flask自带的常用组件介绍

    Flaskrender_templatesessionurl_forredirectflashmake_responsejsonifyblueprintrequestabortgsend_from_d ...

  7. HTML编码和CSS编码会遇到的问

    http://codeguide.bootcss.com/#html-syntax  参考链接 属性顺序 HTML 属性应当按照以下给出的顺序依次排列,确保代码的易读性. class id, name ...

  8. java8完全解读二

    继续着上次的java完全解读一 继续着上次的java完全解读一1.强大的Stream API1.1什么是Stream1.2 Stream操作的三大步骤1.2.1 创建Stream1.2.2 Strea ...

  9. 如何找某个样式属于哪个Element

    如果找不到样式所在的Element,那么可以参考排除法,逐个删除覆盖在同一位置的元素,如果该样式消失,那么可以判断为这个样式.

  10. spring容器和springmvc容器,以及web容器的关系

    说到spring和springmvc,其实有很多人分不清他们有什么区别,认为它俩是一样的,如果你问他项目里用的什么MVC技术,他会说我们用的spring和mybatis,或者spring和hibern ...