当前位置:网站首页>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]
边栏推荐
- STL tutorial 8-map
- MySQL searches and sorts out common methods according to time
- OpenGL shader use
- OpenGL 绘制彩色的三角形
- previous permutation lintcode51
- Pragma pack syntax and usage
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- How to convert a numeric string to an integer
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- 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.".
猜你喜欢

cgroup简介

vulnhub之presidential

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

CGroup introduction

Ripper of vulnhub

(构造笔记)ADT与OOP

PHP导出word方法(一phpword)
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

Laravel time zone timezone

Vulnhub geminiinc
随机推荐
vulnhub之presidential
Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
Go language to realize static server
Vulnhub's Tomato (tomato)
Cacti监控Redis实现过程
(construction notes) grasp learning experience
Groovy test class and JUnit test
MCDF实验1
Solution à la défaillance de l'installation d'Electron
《剑指offer 04》二维数组查找
Vulnhub's Nagini
Simple factory and factory method mode
Why can't my MySQL container start
previous permutation lintcode51
SLF4J 日志门面
Oracle advanced (I) realize DMP by expdp impdp command
023(【模板】最小生成树)(最小生成树)
PHP导出word方法(一mht)
Groovy测试类 和 Junit测试
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022