正则表达式及常用类

来源:互联网 发布:国外数据新闻案例分析 编辑:程序博客网 时间:2024/06/01 17:02

1:正则表达式(理解)

(1)就是符合一定规则的字符串

(2)常见规则

A:字符

x 字符 x。举例:'a'表示字符a

\\ 反斜线字符。

\n 新行(换行)符 ('\u000A') 

\r 回车符 ('\u000D')

B:字符类

[abc] a、b 或 c(简单类) 

[^abc] 任何字符,除了 a、b 或 c(否定) 

[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 

[0-9] 0到9的字符都包括

C:预定义字符类

. 任何字符。我的就是.字符本身,怎么表示呢?   \.

\d 数字:[0-9]

\w 单词字符:[a-zA-Z_0-9]

在正则表达式里面组成单词的东西必须有这些东西组成


D:边界匹配器

^ 行的开头 

$ 行的结尾 

\b 单词边界

就是不是单词字符的地方。

举例:hello world?haha;xixi

E:Greedy 数量词 

X? X,一次或一次也没有

X* X,零次或多次

X+ X,一次或多次

X{n} X,恰好 n 次 

X{n,} X,至少 n 次 

X{n,m} X,至少 n 次,但是不超过 m 次 

(3)常见功能:(分别用的是谁呢?)

A:判断功能

String类的public boolean matches(String regex)

B:分割功能

String类的public String[] split(String regex)

C:替换功能

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

D:获取功能

Pattern和Matcher

Pattern p = Pattern.compile("a*b");

Matcher m = p.matcher("aaaaab");

find():查找存不存在

group():获取刚才查找过的数据

(4)案例

A:判断电话号码和邮箱

B:按照不同的规则分割数据

C:把论坛中的数字替换为*

D:获取字符串中由3个字符组成的单词


#######################################################################################


2:Math(掌握)

(1)针对数学运算进行操作的类

(2)常见方法(自己补齐)

A:绝对值

B:向上取整

C:向下取整

D:两个数据中的大值

E:a的b次幂

F:随机数

G:四舍五入

H:正平方根

(3)案例:

A:猜数字小游戏

B:获取任意范围的随机数

import java.util.Scanner;public class Test_2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入起始数:");int start = sc.nextInt();System.out.println("请输入结尾数:");int end = sc.nextInt();int num = getRandom(start, end);System.out.println(num);}public static int getRandom(int start, int end) {return (int) (Math.random() * (end - start + 1)) + start;}}



#####################################################################################

3:Random(理解)

(1)用于产生随机数的类

(2)构造方法:

A:Random() 默认种子,每次产生的随机数不同

B:Random(long seed) 指定种子,每次种子相同,随机数就相同

(3)成员方法:

A:int nextInt() 返回int范围内的随机数

B:int nextInt(int n) 返回[0,n)范围内的随机数

import java.util.Random;public class Test_2 {public static void main(String[] args) {Random r = new Random();for (int i = 0; i < 10; i++) {System.out.println(r.nextInt(100));}}}


4:System(掌握)

(1)系统类,提供了一些有用的字段和方法。不能被实例化

(2)成员方法(自己补齐)

A:运行垃圾回收器

public static void gc()


B:退出jvm

public static void exit(int status)

终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。


C:获取当前时间的毫秒值

public static long currentTimeMillis()

public class Test_2 {public static void main(String[] args) {long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {System.out.println(i);}long end = System.currentTimeMillis();System.out.println("共用时:" + (end - start) + "毫秒");}}

输出for循环运行消耗的时间。


D:数组复制

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。

import java.util.Arrays;public class Test_2 {public static void main(String[] args) {int[] arr = { 11, 22, 33, 44, 55 };int[] arr2 = { 1, 2, 3, 4, 5 };// 从原数组1号坐标开始复制两个元素到新数组中,从坐标2开始System.arraycopy(arr, 1, arr2, 2, 2);System.out.println(Arrays.toString(arr));System.out.println(Arrays.toString(arr2));}}

输出:

[11, 22, 33, 44, 55]

[1, 2, 22, 33, 5]



5:BigInteger(理解)

(1)针对大整数的运算

(2)构造方法

BigInteger(String val) 

import java.math.BigDecimal;public class Test_2 {public static void main(String[] args) {BigDecimal bi = new BigDecimal("1234567890123");System.out.println(bi);}}

输出:

1234567890123


A:BigInteger(String s)

(3)成员方法(自己补齐)

A:加

public BigInteger add(BigInteger val)

B:减

public BigInteger subtract(BigInteger val)

C:乘

public BigInteger multiply(BigInteger val)

D:除

public BigInteger divide(BigInteger val)

E:商和余数

public BigInteger[] divideAndRemainder(BigInteger val)


测试:

import java.math.BigDecimal;public class Test_2 {public static void main(String[] args) {BigDecimal bi1 = new BigDecimal("11");BigDecimal bi2 = new BigDecimal("5");System.out.println("add:" + bi1.add(bi2));System.out.println("subtract:" + bi1.subtract(bi2));System.out.println("multiply:" + bi1.multiply(bi2));System.out.println("divide:" + bi1.divide(bi2));BigDecimal[] bis = bi1.divideAndRemainder(bi2);System.out.println("商:" + bis[0]);System.out.println("余数:" + bis[1]);}}

输出:

add:16

subtract:6

multiply:55

divide:2.2

商:2

余数:1


6:BigDecimal(理解)

(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)

float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。

BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。

test:

public class Test_2 {public static void main(String[] args) {System.out.println(0.09 + 0.01);System.out.println(1.0 - 0.32);System.out.println(1.015 * 100);System.out.println(1.301 / 100);System.out.println(1.0 - 0.12);}}

输出:

0.09999999999999999

0.6799999999999999

101.49999999999999

0.013009999999999999

0.88


(2)构造方法

public BigDecimal(String val)

(3)成员方法:

A:加

public BigDecimal add(BigInteger val)

B:减

public BigDecimal subtract(BigInteger val)

C:乘

public BigDecimal multiply(BigInteger val)

D:除

public BigDecimal divide(BigInteger val)

E:自己保留小数几位

public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

商   几位小数   如何舍取


test:

import java.math.BigDecimal;public class Test_2 {public static void main(String[] args) {BigDecimal bd1 = new BigDecimal("0.09");BigDecimal bd2 = new BigDecimal("0.01");System.out.println("add:" + bd1.add(bd2));System.out.println("subtract:" + bd1.subtract(bd2));System.out.println("multiply:" + bd1.multiply(bd2));System.out.println("divide:" + bd1.divide(bd2));System.out.println("divide2:" + bd1.divide(bd2, 3, BigDecimal.ROUND_HALF_UP));}}

输出:

add:0.10

subtract:0.08

multiply:0.0009

divide:9

divide2:9.000



7:Date/DateFormat(掌握)

(1)Date是日期类,可以精确到毫秒。

A:构造方法

Date()    根据当前的默认毫秒值创建日期对象

import java.util.Date;public class Test_2 {public static void main(String[] args) {Date d = new Date();System.out.println(d);}}

输出:

Mon Mar 28 11:29:10 CST 2016

周一 3月28日     时区  



Date(long time)    根据给定的毫秒值创建日期对象

Test:

import java.util.Date;public class Test_2 {public static void main(String[] args) {Date d = new Date();System.out.println(d);// 输出起始时间,因为北京是东八区,所以加8个小时System.out.println(new Date(0));long time = 1000 * 60 * 60;//从起始时间增加一小时Date d2 = new Date(time);System.out.println(d2);}}

输出:

Mon Mar 28 11:59:39 CST 2016

Thu Jan 01 08:00:00 CST 1970

Thu Jan 01 09:00:00 CST 1970


B:成员方法

getTime()

获取时间,以毫秒为单位


setTime(long time)    设置时间


Test:

import java.util.Date;public class Test_2 {public static void main(String[] args) {Date d = new Date();//获取时间long time=d.getTime();System.out.println(time);//设置时间System.out.println(d);d.setTime(1000);//增加一秒System.out.println(d);}}

输出:

1459138107911

Mon Mar 28 12:08:27 CST 2016

Thu Jan 01 08:00:01 CST 1970


C:日期和毫秒值的相互转换


(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat 


SimpleDateFormat() 默认模式


A:SimpleDateFormat(String pattern) 给定模式

yyyy-MM-dd HH:mm:ss


Test:

import java.text.SimpleDateFormat;import java.util.Date;public class Test_2 {public static void main(String[] args) {Date d = new Date();// 创建格式化对象SimpleDateFormat sdf = new SimpleDateFormat();SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 调方法默认格式化输出String s = sdf.format(d);String s2 = sdf2.format(d);System.out.println(s);System.out.println(s2);}}

输出:

16-3-28 下午12:32

2016年03月28日 12:32:44


B:日期和字符串的转换

a:Date -- String

format()

b:String -- Date

parse()


test:

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateFormatDemo {public static void main(String[] args) throws ParseException {// Date -- String// 创建日期对象Date d = new Date();// 创建格式化对象// SimpleDateFormat sdf = new SimpleDateFormat();// 给定模式SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// public final String format(Date date)String s = sdf.format(d);System.out.println(s);//String -- DateString str = "2008-08-08 12:12:12";//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dd = sdf2.parse(str);System.out.println(dd);}}

输出:

2016年03月28日 12:40:08

Fri Aug 08 12:12:12 CST 2008


C:案例:

制作了一个针对日期操作的工具类。


工具类:

package bao_01;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * 日起和字符串互换的工具类 * @author super * */public class DateUtil {private DateUtil(){}/** * 方法用于日起转换成一个字符串 * @param d被转换的日期对象 * @param format传递来的要被转换的格式 * @return格式化后的字符串 */public static String dateToString(Date d,String format){//SimpleDateFormat sdf=new SimpleDateFormat(format);return new SimpleDateFormat(format).format(d);}/** * 将字符串转换成日期对象 * @param s被解析的字符串 * @param format要转化的格式 * @return转换后的日期对象 * @throws ParseException抛出异常 */public static Date stringToDate(String s,String format) throws ParseException{return new SimpleDateFormat(format).parse(s);}}


Demo:

package bao_01;import java.text.ParseException;import java.util.Date;public class Test {public static void main(String[] args) throws ParseException {Date d = new Date();// yyyy-MM-dd HH:mm:ssString s = DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");System.out.println(s);String s2 = DateUtil.dateToString(d, "yyyy-MM-dd");System.out.println(s2);String s3 = DateUtil.dateToString(d, "HH:mm:ss");System.out.println(s3);String ss = "2012-12-12 12:12:12";Date d2 = DateUtil.stringToDate(ss, "yyyy-MM-dd HH:mm:ss");System.out.println(d2);Date d3 = DateUtil.stringToDate(ss, "yyyy-MM-dd");System.out.println(d3);//报错,无法识别格式//Date d4 = DateUtil.stringToDate(ss, "HH:mm:ss");//System.out.println(d4);}}

输出:

2016-03-28 13:00:45

2016-03-28

13:00:45

Wed Dec 12 12:12:12 CST 2012

Wed Dec 12 00:00:00 CST 2012


案例:你来到这个世界多少天了?

用工具类实现:

package bao_01;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/*** *  * @author super * */public class YearOld {private YearOld() {}public static long stringToDate(String s, String format) throws ParseException {//通过用户输入得到用户的出生日期Date d = new SimpleDateFormat(format).parse(s);//获取一个当前的日期Date dd = new Date();//用当前日期得到的毫米数减用户的,再计算出天数return (dd.getTime() - d.getTime()) / (1000 * 60 * 60 * 24);}}


Demo:

package bao_01;import java.text.ParseException;import java.util.Scanner;public class Test_2 {public static void main(String[] args) throws ParseException {Scanner sc=new Scanner(System.in);System.out.println("请输入你的出生年月日:");String s=sc.nextLine();long days=YearOld.stringToDate(s, "yyyy-MM-dd");int years=(int)days/365;System.out.println(days);System.out.println(years);}}

输出:

请输入你的出生年月日:

1949-10-01

24285

66



8:Calendar(掌握)

(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。

(2)如何得到一个日历对象呢?

Calendar rightNow = Calendar.getInstance();

本质返回的是子类对象


 Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

 * 

 public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。


test:

import java.util.Calendar;public class test_3 {public static void main(String[] args) {// 获取当前日历时间     子类对象Calendar now = Calendar.getInstance();// 获取年int year = now.get(Calendar.YEAR);// 获取月int month = now.get(Calendar.MONTH);// 获取日int day = now.get(Calendar.DATE);//因为MONTH从0开始,所以需要加一才是当前月System.out.println(year + "年" + (month+1) + "月" + day + "日");}}

输出:

2016年3月29日


(3)成员方法

A:根据日历字段得到对应的值

B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值

C:设置日历对象的年月日


Test:

import java.util.Calendar;public class test_3 {public static void main(String[] args) {// 获取当前日历时间 子类对象Calendar now = Calendar.getInstance();// 获取年int year = now.get(Calendar.YEAR);// 获取月int month = now.get(Calendar.MONTH);// 获取日int day = now.get(Calendar.DATE);// 因为MONTH从0开始,所以需要加一才是当前月System.out.println(year + "年" + (month + 1) + "月" + day + "日");// 年数增加20now.add(Calendar.YEAR, 20);year = now.get(Calendar.YEAR);System.out.println(year + "年" + (month + 1) + "月" + day + "日");// 设置年月日,因为month从0开始,所以输出的是比设定的增加一个月的月份now.set(2012, 12, 12);// 获取年year = now.get(Calendar.YEAR);// 获取月month = now.get(Calendar.MONTH);// 获取日day = now.get(Calendar.DATE);System.out.println(year + "年" + (month + 1) + "月" + day + "日");}}

输出:

2016年3月29日

2036年3月29日

2013年1月12日



(4)案例:

计算任意一年的2月份有多少天?

import java.util.Calendar;public class class4 {public static void main(String[] args) {Calendar rightNow = Calendar.getInstance();// 设置日历对象的年月日rightNow.set(1984, 2, 1);// 时间往前推一天rightNow.add(Calendar.DATE, -1);// 输出这一天System.out.println(rightNow.get(Calendar.DATE));}}

输出:

29


0 0
原创粉丝点击