当前位置:网站首页>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]
边栏推荐
- Visual Studio 2022下载及配置OpenCV4.5.5
- php 获取文件夹下面的文件列表和文件夹列表
- STL tutorial 8-map
- OPenGL 基本知识(根据自己理解整理)
- Niuniu's team competition
- Shutter: about inheritedwidget
- Deploying WordPress instance tutorial under coreos
- 《剑指offer 04》二维数组查找
- Socket TCP for network communication (I)
- OpenGL index cache object EBO and lineweight mode
猜你喜欢
vulnhub之raven2
Raven2 of vulnhub
Is BigDecimal safe to calculate the amount? Look at these five pits~~
836. Merge sets (day 63) and search sets
shardingSphere分库分表<3>
Visual Studio 2022下载及配置OpenCV4.5.5
Socket TCP for network communication (I)
MCDF Experiment 1
rxjs Observable filter Operator 的实现原理介绍
PHP export word method (one MHT)
随机推荐
2022年湖南工学院ACM集训第二次周测题解
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Wrong arrangement (lottery, email)
Vulnhub narak
vulnhub之GeminiInc
C language improvement article (wchar_t) character type
Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
OpenGL 着色器使用
利用Zabbix动态监控磁盘I/O
Solutions to the failure of installing electron
SLF4J 日志门面
CGroup introduction
Vulnhub's presidential
《剑指offer 04》二维数组查找
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
牛牛的组队竞赛
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
SystemVerilog -- OOP -- copy of object
[MySQL special] read lock and write lock
Visual Studio 2022下载及配置OpenCV4.5.5