当前位置:网站首页>正则表达式总结
正则表达式总结
2022-06-25 18:16:00 【qq_45071235】
目录
正则表达式(regular expression)描述了一种 字符串匹配 的模式(pattern),可以用来检 查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。
1.提取字符串中的有用信息:
public static void main(String[] args) {
String url = "http://dyit:8663/pets?username=xjaih458&password=456";
String[] h = new String[3];
String[] s = url.split("[?]");
for (String st : s) {
System.out.println(st+"\t");
}
String[] s1 = s[1].split("[&]");
h[2] = s1[1].substring(s1[1].indexOf("=")+1);
h[1] = s1[0].substring(s1[0].indexOf("=")+1);
h[0] = s[0].substring(s[0].indexOf("dyit:")+"dyit:".length(),s[0].indexOf("/pets"));
for (String st : h) {
System.out.print(st+"\t");
}
}
2.判断密码输入:
public static void main(String[] args) {
String password = "123546";
String regex = "[0-9]{6}";//匹配密码
System.out.println(password.matches(regex));
//匹配手机号码
String num = "17623586478";
String regex1 = "[1][0-9]{10}";
System.out.println(num.matches(regex1));
//匹配身份证号码
String num1 = "610056888785642682";
String regex2 = "[1-9][0-9]{16}[0-9X]";
System.out.println(num1.matches(regex2));
String password1 = "123456zxc789";
String regex3 = "[0-9a-z]{6,}";
System.out.println(password1.matches(regex3));
String regex4 = "[0-9a-z]{6,11}";
System.out.println(password1.matches(regex4));
//至少
String str = "";
// String regex5 = "[0-9]+";
String regex6 = "\\d*";// \w \D
System.out.println(str.matches(regex6));
String regex7 = "[0-9]?";//?表示0次或1次
System.out.println(str.matches(regex7));
//XY与X|Y关系
String gender = "保密";
String regex8 = "[男]|[女]|(保密)";
System.out.println(gender.matches(regex8));
}总结:
点号(.): 匹配除“\n”之外的任何单个字符。
数字字符\d:匹配一个数字字符。等价于[0-9]。
非数字字符\D:匹配一个非数字字符。等价于[ˆ0-9]。
单词字符\w:匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]”
非单词字符\W :匹配任何非单词字符。等价于“[ˆA-Za-z0-9_]”。
*号:匹配前面的子表达式 零次或多次 。
+号 :匹配前面的子表达式 一次或多次 。
?号 :匹配前面的子表达式 零次或一次 。
{n}:n是一个非负整数。匹配确定的 正好n次 。
{n,}:n是一个非负整数。匹配 至少n次 。
{m,n} :m,n是非负整数。匹配 至少m次,至多n次 。
XY匹配 :先匹配X再匹配Y
X|Y匹配:匹配X或者匹配Y,(group)括号表示一组
3.提取中文,int转化为String,字符串拼接
String str = "8w5wf4f7wfe1在aaj去s56a4卡爱好5s";
String regex = "[^\u4e00-\u9fa5]";//提取中文
String s = str.replaceAll(regex, "");
System.out.println(s);
int a = 5;//int转换为String
String s1 = String.valueOf(a);
String b = "it's my book";
//查找子字符串
System.out.println("indexOf=" +b.indexOf("book"));
System.out.println("endsWith=" +b.endsWith("book"));
System.out.println("concat=" +b.contains("book"));
//字符串拼接
String k = "tom";
String l = "good";
String j = k+l;
System.out.println("+--->"+j);
System.out.println("concat--->"+ k.concat(l));
System.out.println("join--->"+ String.join("?", k,l));4.在一个字符串中查找某个字符串的个数(四个方法)
//第一组方法
String h = str1.replaceAll("apple", "#");
System.out.println(h.replaceAll("[^#]", "").length());
//第二种方法
int index = 0;
int count = 0;
while(true){
index = str1.indexOf("apple",index);
if(index == -1) {
break;
}else {
count++;
index+="apple".length();
}
}
System.out.println("apple="+ count);
//第三种方法
int o = 0;
String[] p = str1.split("[ ,]");
for (String st : p) {
System.out.print(st);
if(st.contains("apple")) {
o++;
}
}
System.out.println("\napple:"+ o);
d(str1);
System.out.println(u);
}
//递归方法
static int u = 0;
public static void d(String str) {
int index1 = str.indexOf("apple");
if(index1 == -1) {
return ;
}
u++;
d(str.substring(index1+"apple".length()));
}
5.输入一个字符串,统计每个字符出现的次数
int[] arr = new int[127];
String str="fhjghftgvgghgvgvfgj$^&#@()*!as12";
for(int i = 0;i<str.length();i++) {
char ch = str.charAt(i);
arr[ch] +=1;
}
System.out.println(Arrays.toString(arr));
int pos = 0;
for(int i = 0;i<arr.length;i++) {
if(arr[pos]<arr[i]) {
pos=i;
}
System.out.println((char)(i)+"出现:"+arr[i]+"次");
}
System.out.println("出现最多的是:"+(char)pos);

6.输入两个字符串判断一下是否异构
String a = "hello";
String b = "hoell";
int[] chs1= new int[26];
int[] chs2= new int[26];
for(int i = 0;i<a.length();i++) {
char ch1 = a.charAt(i);
chs1[ch1-'a']+=1;
char ch2 = b.charAt(i);
chs2[ch2-'a']+=1;
}
System.out.println(Arrays.toString(chs1));
System.out.println(Arrays.toString(chs2));
System.out.println(Arrays.equals(chs1, chs2));
//排序做法
char[] chs3 = a.toCharArray();
char[] chs4 = b.toCharArray();
Arrays.sort(chs3);//排序
System.out.println(Arrays.toString(chs3));
Arrays.sort(chs4);
System.out.println(Arrays.toString(chs4)); 
边栏推荐
- 哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码
- Matlab中图形对象属性gca使用
- Interrupt operation: abortcontroller learning notes
- OSError: Unable to open file (truncated file: eof = 254803968, sblock->base_addr = 0, stored_eof = 2
- . How to exit net worker service gracefully
- 【深入理解TcaplusDB技术】Tmonitor系统升级
- 【深入理解TcaplusDB技术】 Tmonitor模块架构
- [in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
- Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
- Leetcode force buckle (Sword finger offer 26-30) 26 Substructure of tree 27 Image of binary tree 28 Symmetric binary tree 29 Print matrix 30 clockwise Stack containing min function
猜你喜欢

【深入理解TcaplusDB技术】 Tmonitor模块架构

QT中QString包含“\u0000“的处理方式

Solve nvprof error err_ NVGPUCTRPERM - The user does not have permission to profile on the target device.

解析数仓lazyagg查询重写优化

What is an operator?

揭秘GES超大规模图计算引擎HyG:图切分
![[deeply understand tcapulusdb technology] create a game zone](/img/91/cf4eae9a4336ca407c0da805b9d909.png)
[deeply understand tcapulusdb technology] create a game zone

158_ Model_ Power Bi uses DAX + SVG to open up almost all possibilities for making business charts

.NET Worker Service 如何优雅退出

什么是算子?
随机推荐
CentOS7 安装 Redis 7.0.2
[how do I refresh the shuttle page after jumping back?]
【深入理解TcaplusDB技术】TcaplusDB运维单据
Dell r530 built in hot spare status change description
Is it convenient to open a stock account? Is online account opening safe?
SQL Server实时备份库要求
General message publishing and subscription in observer mode
Iet attends the 2022 World Science and technology community development and Governance Forum and offers suggestions for building an international science and technology community
RMAN备份数据库_双重备份备份集(Duplexing Backup Sets)
什么是算子?
广发易淘金和指南针哪个更好,更安全一些
How can the self-supporting number evaluation be safer for cross-border e-commerce platforms such as Amazon, eBay, shopee, lazada, express, Wal Mart and Alibaba international?
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
GNU nano
使用宝塔来进行MQTT服务器搭建
RMAN备份数据库_使用RMAN做拆分镜像(split mirror)备份
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
[in depth understanding of tcapulusdb technology] tcapulusdb model
What is an operator?
.NET Worker Service 添加 Serilog 日志记录