当前位置:网站首页>正则表达式总结
正则表达式总结
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)); 
边栏推荐
- IVX sailing
- 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?
- 哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码
- RMAN备份数据库_重启RMAN备份
- Is the actual account opening complicated? Is online account opening safe?
- 延时函数如何延时
- Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
- IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
- 篇7:CLion中没有代码提示,,,
- LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
猜你喜欢

【深入理解TcaplusDB技术】TcaplusDB导入数据

.NET Worker Service 如何优雅退出

視覺SLAM十四講 第9講 卡爾曼濾波

ASP. Net supermarket convenience store online shopping mall source code, for the surrounding distribution system

Article 6:clion:toolchains are not configured configure disable profile

.NET Worker Service 添加 Serilog 日志记录
![There is a repeating element iii[pruning with ordered ordering]](/img/26/5c3632a64945ea3409f8240ef5b18a.png)
There is a repeating element iii[pruning with ordered ordering]

Dell r530 built in hot spare status change description

【深入理解TcaplusDB技术】单据受理之建表审批

Article 7: there is no code prompt in clion,,,
随机推荐
Part 5:vs2017 build qt5.9.9 development environment
什么是泛型以及在集合中泛型的使用[通俗易懂]
1. Understanding of norm
[deeply understand tcapulusdb technology] create a game zone
What is an operator?
Pycharm 使用过程中碰到问题
Anaconda download Tsinghua source
【深入理解TcaplusDB技术】单据受理之表管理
Solve nvprof error err_ NVGPUCTRPERM - The user does not have permission to profile on the target device.
延时函数如何延时
How to open a stock account? Is it safe to open a securities account
QT中QString包含“\u0000“的处理方式
[deeply understand tcapulusdb technology] tcapulusdb import data
【深入理解TcaplusDB技術】TcaplusDB業務數據備份
Handling method of qstring containing "\u0000" in QT
C ASP, net core framework value transfer method and session use
CentOS7 安装 Redis 7.0.2
Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
Is it safe for a securities company to open an account with the lowest handling fee among the top ten
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance