当前位置:网站首页>Regular expression summary
Regular expression summary
2022-06-25 18:29:00 【qq_ forty-five million seventy-one thousand two hundred and thi】
Catalog
Regular expressions (regular expression)
1. Extract useful information from a string :
3. Extract Chinese ,int Turn into String, String splicing
4. Find the number of a string in a string ( Four ways )
5. Enter a string , Count the number of occurrences of each character
6. Enter two strings to determine whether they are heterogeneous
Regular expressions (regular expression) It describes one kind string matching The pattern of (pattern), Can be used to check Check whether a string contains a certain seed string 、 Replace a matched substring or remove a substring from a string that meets a condition, etc .
1. Extract useful information from a string :
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. Judge the password input :
public static void main(String[] args) {
String password = "123546";
String regex = "[0-9]{6}";// Match the password
System.out.println(password.matches(regex));
// Match cell phone number
String num = "17623586478";
String regex1 = "[1][0-9]{10}";
System.out.println(num.matches(regex1));
// Match ID number
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));
// At least
String str = "";
// String regex5 = "[0-9]+";
String regex6 = "\\d*";// \w \D
System.out.println(str.matches(regex6));
String regex7 = "[0-9]?";//? Express 0 Time or 1 Time
System.out.println(str.matches(regex7));
//XY And X|Y Relationship
String gender = " A secret ";
String regex8 = "[ male ]|[ Woman ]|( A secret )";
System.out.println(gender.matches(regex8));
}summary :
Order number (.): Matching elimination “\n” Any single character other than .
Numeric character \d: Matches a numeric character . Equivalent to [0-9].
Nonnumeric character \D: Matches a non-numeric character . Equivalent to [ˆ0-9].
Word characters \w: Match any word character that includes an underline . Equivalent to “[A-Za-z0-9_]”
Nonword character \W : Match any non word character . Equivalent to “[ˆA-Za-z0-9_]”.
* Number : Match the previous subexpression Zero or more times .
+ Number : Match the previous subexpression Once or more .
? Number : Match the previous subexpression Zero or one .
{n}:n Is a non negative integer . Matched definite Just right n Time .
{n,}:n Is a non negative integer . matching At least n Time .
{m,n} :m,n Is a nonnegative integer . matching At least m Time , at most n Time .
XY matching : First match X Match again Y
X|Y matching : matching X Or match Y,(group) Brackets denote a set of
3. Extract Chinese ,int Turn into String, String splicing
String str = "8w5wf4f7wfe1 stay aaj Go to s56a4 Card hobby 5s";
String regex = "[^\u4e00-\u9fa5]";// Extract Chinese
String s = str.replaceAll(regex, "");
System.out.println(s);
int a = 5;//int Convert to String
String s1 = String.valueOf(a);
String b = "it's my book";
// Find substrings
System.out.println("indexOf=" +b.indexOf("book"));
System.out.println("endsWith=" +b.endsWith("book"));
System.out.println("concat=" +b.contains("book"));
// String splicing
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. Find the number of a string in a string ( Four ways )
// The first set of methods
String h = str1.replaceAll("apple", "#");
System.out.println(h.replaceAll("[^#]", "").length());
// The second method
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);
// The third method
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);
}
// Recursive method
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. Enter a string , Count the number of occurrences of each character
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)+" appear :"+arr[i]+" Time ");
}
System.out.println(" What appears most is :"+(char)pos);

6. Enter two strings to determine whether they are heterogeneous
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));
// Sorting method
char[] chs3 = a.toCharArray();
char[] chs4 = b.toCharArray();
Arrays.sort(chs3);// Sort
System.out.println(Arrays.toString(chs3));
Arrays.sort(chs4);
System.out.println(Arrays.toString(chs4)); 
边栏推荐
- LeetCode 3. Longest substring without repeated characters & sliding window
- Android Internet of things application development (smart Park) - picture preview interface
- 03 runtime data area overview and threads
- 中断操作:AbortController学习笔记
- Differences and relations between sequential table and array (easy to confuse), linear table and linked list
- [in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
- Meaning of% in C language
- IVX 启航
- 使用宝塔来进行MQTT服务器搭建
- C ASP, net core framework value transfer method and session use
猜你喜欢

Handling method of qstring containing "\u0000" in QT

安装spark + 用命令运行scala相关项目 + crontab定时执行

General message publishing and subscription in observer mode

anaconda下载清华源
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/31/4e33fafa090e0bb5b55e11978cdff8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

Chapter 4:win10 installing mingw64

1. Understanding of norm
![[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance](/img/e5/9c902f5d6aaeb7113e82d2c1084005.png)
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance

03 runtime data area overview and threads

. How to exit net worker service gracefully
随机推荐
Network security algorithm
RMAN backup database_ Manage backup window
Chapter 4:win10 installing mingw64
Batch uploading of local jar packages to nexus private server
LeetCode 322. Change exchange & dynamic planning
06 local method interface
Meaning of% in C language
【深入理解TcaplusDB技术】TcaplusDB运维
IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
OSError: Unable to open file (truncated file: eof = 254803968, sblock->base_addr = 0, stored_eof = 2
【深入理解TcaplusDB技术】TcaplusDB构造数据
Use pagoda to set up mqtt server
JSP page running but displaying source code
Kwai 616 war report was launched, and the essence was thrown away for the second time to lead the new wave. Fast brand jumped to the top 3 of the hot list
Training of long and difficult sentences in postgraduate entrance examination day88
IVX sailing
JSP页面运行却显示源码
Android物联网应用程序开发(智慧园区)—— 图片预览界面
Article 6:clion:toolchains are not configured configure disable profile
Training of long and difficult sentences in postgraduate entrance examination day81