栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-11-01

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-11-01

java正则表达式

在java.util.regex包里面定义了两个主要的类:
·Pattern类:此类对象如果要想使用必须使用compile()方法,方法的功能是编译正则
public static Pattern compile(String regex);
·Matcher类:通过Pattern类取得
public Matcher matcher(CharSequence input);
String stb = “heiaiwedhfa4ae6df54asfdaewadfes”;
Pattern compile = Pattern.compile("[e]");//Pattern类通过static方法拿到Pattern类的对象
Matcher m = compile.matcher(stb);//Matcher通过Pattern的方法matcher(CharSequence input)拿到Matcher类的对象
while(m.find()) {//Matcher类对象通过find()方法得到取得的结果
System.out.println(m.group());Matcher类对象通过group()方法输出结果
}

3.2、正则标记
1、单个字符(数量:1)
·字符:表示由一个字符组成
·:表示转义字符“”
·t:表示一个“t”符号
·n:表示匹配换行(n)
2、字符集(数量:1)
·[abc]:表示可能是字符a、b、c中的任意一位字符
·[^abc]:表示不是a、b、c中的任意一位字符
·[a-z]:表示所有的小写字母
·[a-z A-Z]:表示任意的一位字母,不区分大小写
·[0-9]:表示任意一位数字
3、简化的字符集表达式(数量:1)
·.:表示任意的一位字符
·d:等价于[0-9]
·D:等价于[^0-9]
·s:表示任意的一位空白字符例如:“t”、“n”
·S:表示任意的一位非空白字符
·w:等价于[a-z A-Z _ 0-9],表示由任意的字母数字_所组成
·W: 等价于[^a-z A-Z _ 0-9],表示不是由任意的字母数字_所组成
4、边界匹配(在java script中使用)
^:正则的开始
$:正则的结束
5、数量表达式
正则?:表示此正则可以出现0或1次
正则+:表示此正则可以出现1次或多次
正则*:表示此正则可以出现0、1次或多次
正则{n}:表示次正则正好出现n次
正则{n,}:表示次正则正好出现n次或n次以上
正则{n,m}:表示此正则出现n~m次
6、逻辑运算
正则1正则2:正则1判断完成之后继续判断正则2
正则1|正则2:正则1或正则2有一组满足即可
(正则):将多个正则作为一组,可以为这一组单独设置出现的次数
3.3、String类对正则的支持
在jdk1.4之后,由于正则的引入,所以String类里面也相应的增加了新的操作方法支持
public boolean matches(String regex):正则验证,使用指定的字符串判断其是否符合给出的的正则表达式结构
public String replaceAll(String regex, String replacement):全部替换
public String replaceFirst(String regex, String replacement):替换首个
public String[] split(String regex):全部拆分
public String[] split(String regex, int limit):部分拆分

范例:实现字符串替换
public class TestDemo {
public static void main(String[] args) {
String str = “weldsafk*)()*asdlkfjao#$afdklja” ;
String regex ="[^a-z]";
System.out.println(str.replaceAll(regex,""));
}
}
范例:字符串拆分
public class TestDemo {
public static void main(String[] args) {
String str = “lak8dfja5644asdf99ja” ;
String regex ="d+";
String result [] = str.split(regex);
for(int x = 0 ;x < result.length ;x ++) {
System.out.println(result[x]);
}
}
}
所有正则之中最应该兴奋的事情是因为可以使用它进行验证
范例:验证一个字符串是否是数字,如果是将其变为double型
public class TestDemo {
public static void main(String[] args) {
String str = “10.2” ;
String regex ="d+(.d+)?";
System.out.println(str.matches(regex));
if(str.matches(regex)) {
System.out.println(Double.parseDouble(str));
}
}
}
范例:判断给定的字符串是否是IP地址(IPV4)
public class TestDemo {
public static void main(String[] args) throws Exception{
String str = “192.168.1.1” ;
String regex ="(d{1,3}.){3}d{1,3}";
System.out.println(str.matches(regex));
}
}
范例:给定一个字符串判断是否是日期格式,如果是则将其转换为Data型数据
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDemo {
public static void main(String[] args) throws Exception{
String str = “2009-03-23” ;
String regex ="d{4}-d{2}-d{2}";
System.out.println(str.matches(regex));
if(str.matches(regex)) {
Date data = new SimpleDateFormat(“yyyy-MM-dd”).parse(str);
System.out.println(data);
}
}
}
范例:判断电话号码,一般要编写电话号码一下几种格式都是满足的:
·格式一:51283346
·格式二:010-51283346
·格式三:(010)-51283346
public class TestDemo {
public static void main(String[] args) throws Exception{
String str = “51283346” ;
String regex ="((d{3,4}-)|((d{3,4}))-)?d{7,8}";
System.out.println(str.matches(regex));
}
}
范例:验证email地址
·要求格式一:email由字母、数字、所组成
·要求格式二:用户名要求由字母、数字、
、.组成,其中必须以字母开头和结尾,用户名长度不超过30最后的根域名只能是.com、.cn、.net、.com.cn、.net.cn、.edu、.gov、.org
public class TestDemo {
public static void main(String[] args) throws Exception{
String str = “hello_123@mldn.cn” ;
String regex ="[a-zA-Z][a-zA-Z0-9_.]{0,28}@w+.(net|cn|com|.cn|net.cn|org|gov|edu)";
System.out.println(str.matches(regex));
}
}
范例:验证汉字
public class TestDemo {
public static void main(String[] args) throws Exception{
String sta = “你好”;
String reg = “.+”;
System.out.println(sta.matches(reg));
}
}

带尺寸的图片:

居中的图片:

居中并且带尺寸的图片:

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';
生成一个适合你的列表
  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务
创建一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1
设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列第二列第三列
第一列文本居中第二列文本居右第三列文本居左
SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPEASCIIHTML
Single backticks'Isn't this fun?'‘Isn’t this fun?’
Quotes"Isn't this fun?"“Isn’t this fun?”
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash
创建一个自定义列表
Markdown
Text-to- HTML conversion tool
Authors
John
Luke
如何创建一个注脚

一个具有注脚的文本。1

注释也是必不可少的

Markdown将文本转换为 HTML。

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N Gamma(n) = (n-1)!quadforall ninmathbb N Γ(n)=(n−1)!∀n∈N 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . Gamma(z) = int_0^infty t^{z-1}e^{-t}dt,. Γ(z)=∫0∞​tz−1e−tdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章
  • 关于 甘特图 语法,参考 这儿,
UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

这将产生一个流程图。:

  • 关于 Mermaid 语法,参考 这儿,
FLowchart流程图

我们依旧会支持flowchart的流程图:

  • 关于 Flowchart流程图 语法,参考 这儿.
导出与导入 导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. 注脚的解释 ↩︎

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号