当前位置:网站首页>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]
边栏推荐
猜你喜欢
随机推荐
During FTP login, the error "530 login incorrect.login failed" is reported
Vulnhub geminiinc V2
Vulnhub's Tomato (tomato)
Experience container in libvirt
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Nestjs configuration service, configuring cookies and sessions
为什么我的mysql容器启动不了呢
(数据库提权——Redis)Redis未授权访问漏洞总结
Redis 笔记 01:入门篇
vulnhub之momentum
OpenStack中的测试分类
PHP export word method (one MHT)
Optimize interface performance
Pragma pack syntax and usage
STL Tutorial 9 deep copy and shallow copy of container elements
Download address and installation tutorial of vs2015
Dart: about grpc (I)
SystemVerilog -- OOP -- copy of object
OpenGL shader use
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