当前位置:网站首页>regular expression
regular expression
2022-07-03 12:06:00 【Dull Yanan】
Regular expressions
The basic content
[a-z] matching […] All characters in
[^a-b] Match except […] All the characters of the characters in
[\s\S] Match all .\s Is to match all blanks , Including line breaks ,\S Not blank , Including line breaks .
\w Match the letter 、 Numbers 、 Underline . Equivalent to [A-Za-z0-9_]
. All contents
\d Means match number
\d\d\d It means matching three consecutive numbers , If it's continuous , Then disconnect the match
\d{3} ditto
(\d\d){3} Express 6 Time Do not use parentheses to indicate 4 Time
\d{3,6} least 3 Time , most 6 Time
\d{3,} least 3 Time , There is no upper limit
When matching, it defaults to greedy mode , The more matches, the better
Add one in the back ? Non greedy model
SYN?NN ? The front one N It can appear once or 0 Time
SYN*NN * The front one N Can appear 0 1 2 3…
SYN+NN + Front N appear 1 2 3 4 5 …
a\d?b ab There is 0 perhaps 1 A digital
^ Matches where the string starts ^a Begin to match a
$ Match where the string ends a$ End match a
\b Matches a word boundary
toknnan\b
A single letter , The characters in front of and behind are not all \w
([a-z]{2})\1 Match repeat such as gogo
above \1 It can also be called back reference
Reference the string that the group has captured
(?:Expression) Non capturing group Do not occupy memory
m.group() Method You can group when querying 1 2 3 Represents that the front expression is different () What's in it
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** @Author ToknNan */
public class Test {
public void testMatch1() {
String s1 = "sss15831asdasdasd7762461";
String lamba = "\\w+";
// Expression object
Pattern p = Pattern.compile(lamba);
// A match object
Matcher m = p.matcher(s1);
// Intercept data
boolean ans = m.matches();
boolean ans2=m.find();
String ans2A=m.group();
System.out.println(ans);
System.out.println(ans2);
System.out.println(ans2A);
}
public void testMatch2()
{
String s1 = "sss15831asdasdasd&&7762461";
String lamba = "lasd";
boolean ans=Pattern.matches(lamba,s1);
System.out.println(ans);
}
public void testMatch3()
{
String s="asdf^asdcfe^abcde^123";
String lamba="\\w+";
Pattern p=Pattern.compile(lamba);
Matcher m=p.matcher(s);
while (m.find())
{
System.out.println(m.group());
}
}
/** @author ToknNan @version 1.0.0 group Search function */
public void testMatch4()
{
String s="[email protected]";
String lamba="(\\d+)@(\\w{2,3})";
Pattern p=Pattern.compile(lamba);
Matcher m=p.matcher(s);
System.out.println(m.find());
System.out.println(m.group(1));
System.out.println(m.group(2));
}
/** * Replacement function **/
public void testMatch5()
{
String s="[email protected]";
String lamba="[0-9]";
Pattern p=Pattern.compile(lamba);
Matcher m=p.matcher(s);
String ss=m.replaceAll("&");
System.out.println(ss);
}
/** * Regular expression cuts * */
public void testMatch6()
{
String s="syn1sfasf2gfhg3fgsdg4waf5dasfg6";
String[] arrs=s.split("\\d+");
for (String a:arrs)
{
System.out.println(a);
}
}
}
Common expression
chinese
Chinese characters :[\u4e00-\u9fa5]
Chinese punctuation :[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]
Chinese comma :[\uff0c]
边栏推荐
- Cacti monitors redis implementation process
- Unity3d learning notes 5 - create sub mesh
- Dart: view the dill compiled code file
- Dart: about grpc (I)
- (构造笔记)MIT reading部分学习心得
- How to convert a numeric string to an integer
- Shutter: about inheritedwidget
- Oracle advanced (I) realize DMP by expdp impdp command
- VS2015的下载地址和安装教程
- OpenGL 着色器使用
猜你喜欢

Shardingsphere sub database and sub table < 3 >

(construction notes) ADT and OOP

vulnhub之GeminiInc v2

OpenGL draws colored triangles

解决msvcp120d.dll和msvcr120d.dll缺失

Wrong arrangement (lottery, email)

rxjs Observable filter Operator 的实现原理介绍

836. Merge sets (day 63) and search sets

STL tutorial 10 container commonalities and usage scenarios

cgroup简介
随机推荐
Vulnhub narak
(construction notes) learning experience of MIT reading
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
vulnhub之momentum
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
023(【模板】最小生成树)(最小生成树)
利用Zabbix动态监控磁盘I/O
Go语言实现静态服务器
OpenGL 着色器使用
SLF4J 日志门面
【mysql官方文档】死锁
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
Nestjs configuration service, configuring cookies and sessions
Dynamically monitor disk i/o with ZABBIX
Deploying WordPress instance tutorial under coreos
Wrong arrangement (lottery, email)
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
MCDF实验1
Kubernetes three dozen probes and probe mode
DNS multi-point deployment IP anycast+bgp actual combat analysis