当前位置:网站首页>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)); 
边栏推荐
- Interrupt operation: abortcontroller learning notes
- Redis 5.0 data structure double end linked list source code analysis
- 【深入理解TcaplusDB技术】Tmonitor系统升级
- 06 local method interface
- Part 5:vs2017 build qt5.9.9 development environment
- IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
- Lazy singleton mode from shallow to deep
- [deeply understand tcapulusdb technology] tcapulusdb import data
- Training of long and difficult sentences in postgraduate entrance examination day86
- Use pagoda to set up mqtt server
猜你喜欢

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

【深入理解TcaplusDB技术】单据受理之创建游戏区

1、对范数的理解

Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)

Optimization of lazyagg query rewriting in parsing data warehouse
![[deeply understand tcapulusdb technology] create a game zone](/img/91/cf4eae9a4336ca407c0da805b9d909.png)
[deeply understand tcapulusdb technology] create a game zone

【深入理解TcaplusDB技術】TcaplusDB業務數據備份

Article 7: there is no code prompt in clion,,,

【深入理解TcaplusDB技术】TcaplusDB运维单据

【深入理解TcaplusDB技术】TcaplusDB新增机型
随机推荐
Training of long and difficult sentences in postgraduate entrance examination English Day82
connect to address IP: No route to host
Training of long and difficult sentences in postgraduate entrance examination day83
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Article 7: there is no code prompt in clion,,,
Dell R530内置热备盘状态变化说明
将Graph Explorer搬上JupyterLab:使用GES4Jupyter连接GES并进行图探索
Network security algorithm
Install spark + run Scala related projects with commands + crontab scheduled execution
[in depth understanding of tcapulusdb technology] new models of tcapulusdb
Sword finger offer double pointer
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
Class 02 loader subsystem
Handling method of qstring containing "\u0000" in QT
Redis 5.0 data structure double end linked list source code analysis
华为云SRE确定性运维专刊(第一期)
JVM problem replication
C ASP, net core framework value transfer method and session use
How to sort massive data? How to process data between memory and hard disk?
OSError: Unable to open file (truncated file: eof = 254803968, sblock->base_addr = 0, stored_eof = 2