吴裕雄--天生自然 JAVA开发学习:正则表达式
import java.util.regex.*;
class RegexExample1{
public static void main(String args[]){
String content = "I am noob " +
"from runoob.com.";
String pattern = ".*runoob.*";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
public static void main( String args[] ){ // 按指定模式在字符串查找
String line = "This order was placed for QT3000! OK?";
String pattern = "(\\D*)(\\d+)(.*)"; // 创建 Pattern 对象
Pattern r = Pattern.compile(pattern); // 现在创建 matcher 对象
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
System.out.println("Found value: " + m.group(3) );
} else {
System.out.println("NO MATCH");
}
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat"; public static void main( String args[] ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
int count = 0; while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
private static Matcher matcher2; public static void main( String args[] ){
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
matcher2 = pattern.matcher(INPUT2); System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2); System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
System.out.println("lookingAt(): "+matcher2.lookingAt());
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " +
"All dogs say meow.";
private static String REPLACE = "cat"; public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class RegexMatches
{
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobkkk";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// 获取 matcher 对象
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
}
}

吴裕雄--天生自然 JAVA开发学习:正则表达式的更多相关文章
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- 吴裕雄--天生自然 JAVA开发学习:解决java.sql.SQLException: The server time zone value报错
这个异常是时区的错误,因此只你需要设置为你当前系统时区即可,解决方案如下: import java.sql.Connection ; import java.sql.DriverManager ; i ...
- 吴裕雄--天生自然 JAVA开发学习:基础语法
package test; public class temp { /* 第一个Java程序 * 它将打印字符串 Hello World */ public static void main(Stri ...
- 吴裕雄--天生自然 JAVA开发学习:Scanner 类
import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...
- 吴裕雄--天生自然 JAVA开发学习:流(Stream)、文件(File)和IO
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //使用 BufferedReader 在控制台读取 ...
- 吴裕雄--天生自然 JAVA开发学习:方法
/** 返回两个整型变量数据的较大值 */ public static int max(int num1, int num2) { int result; if (num1 > num2) re ...
- 吴裕雄--天生自然 JAVA开发学习:日期时间
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...
- 吴裕雄--天生自然 JAVA开发学习:String 类
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...
随机推荐
- swoole在线聊天学习笔记
<?php $http=); $http->on('request',function(swoole_http_request $request,swoole_http_response ...
- 第二阶段scrum-7
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 部署云服务器完成,链接数据库完成,消息收发正在制作.
- CGridCtrl显示子控件 及事件
m_Grid.SetCellType(row, , RUNTIME_CLASS(CGridCell)); m_Grid.SetItemText(row, , _T(")); m_Grid.S ...
- Day2-T1
原题目 Describe:贪心,左边和右边中选字典序小的 code: #include<bits/stdc++.h> using namespace std; int n,step,hea ...
- 04-String——课后作业1:字串加密
题目:请编写一个程序,加密或解密用户输入的英文字串要求设计思想.程序流程图.源代码.结果截图. 程序设计思想:首先由用户选择是加密还是解密,利用String类中的charAt函数依次取出字串中的字符, ...
- Pycharm2020最新激活码|永久激活(附最新激活码和插件)
最近很多人的Pycharm激活时间又过期了,后台很多人索要激活码,我就再把激活的方法汇和工具再梳理一次给大家. 最主要有两种激活方式(两种方式需要的激活码不同): 一.激活码激活: 一般一年多需要激活 ...
- Mac下使用Hexo搭建个人博客
Hexo介绍 利用原作者的一句话:A fast,simple&powerful blog framework,powered by Node.js Hexo是基于Node.js的博客平台,He ...
- MySQL授权用户登录访问指定数据库
使用Navicat等客户端工具,选中需要共享的数据库,点击查询>新建查询 1.写SQL语句:GRANT ALL PRIVILEGES ON * TO 'test'@'%'IDENTIFIED B ...
- Cassandra--Cassandra 安装
当前最新版本:3.11.3 https://cassandra.apache.org/doc/latest/getting_started/installing.html 前提条件 安装Java8. ...
- 合并排序_python
#!/usr/bin/python # --coding:utf-8 -- def sort_merge(left,right): i,j=0,0 result=[] while i<len(l ...