1.PCRE表达式全集1
字符 | 描述 |
---|---|
\ | 将下一个字符标记为一个特殊字符、或一个原义字符(有^$()*+?.[\{|共计12个)或一个向后引用或一个八进制转义符 |
^ | 匹配输入字符串的开始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的子表达式零次或无数次,* 等价于{0,} |
+ | 匹配前面的子表达式1次或多次,+等价于{1,} |
? | 匹配前面的子表达式0或1次 |
{n} | 匹配前面的子表达式确定的n次 |
{n,} | n是一个非负整数。至少匹配n次 |
{n,m} | m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次 |
x|y | 没有包围在()里,其范围是整个正则表达式,“z|food”能匹配“z”或“food” |
[xyz] | 字符集合(character class)。匹配所包含的任意一个字符。特殊字符仅有反斜线\保持特殊含义,用于转义字符。其它特殊字符如星号、加号、各种括号等均作为普通字符。脱字符^如果出现在首位则表示负值字符集合;如果出现在字符串中间就仅作为普通字符。连字符 - 如果出现在字符串中间表示字符范围描述;如果如果出现在首位(或末尾)则仅作为普通字符。右方括号应转义出现,也可以作为首位字符出现。 |
[^xyz] | 排除型字符集合,匹配未列出的任意字符。“[^abc]”可以匹配“plain”中的“plin” |
[a-z] | “[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符 |
[^a-z] | 排除型的字符范围,匹配任何不在指定范围内的任意字符 |
\b | 匹配一个单词边界,也就是指单词和空格间的位置。例如,“er\b”可以匹配“never”中的“er”,但不能匹配“verb”中的“er”。 |
\B | 匹配非单词边界。“er\B”能匹配“verb”中的“er”,但不能匹配“never”中的“er”。 |
\d | 匹配一个数字字符。等价于[0-9]。 |
\D | 匹配一个非数字字符。等价于[^0-9]。 |
\f | 匹配一个换页符。等价于\x0c和\cL |
\n | 匹配一个换行符。等价于\x0a和\cJ |
\r | 匹配一个回车符。等价于\x0d和\cM |
\s | 匹配任何空白字符,包括空格、制表符、换页符等等,等价于[ \f\n\r\t\v] |
\S | 匹配任何非空白字符。等价于[^ \f\n\r\t\v] |
\t | 匹配一个制表符。等价于\x09和\cI |
\w | 匹配包括下划线的任何单词字符。等价于“[A-Za-z0-9_]” |
\W | 匹配任何非单词字符。等价于“[^A-Za-z0-9_]” |
() | 用于分组、提取数据(\d{4})-(\d{2})-(\d{2})匹配"2017-06-12"结果为 [“2017-06-12”, “2017”, “06”, “12”] |
. | 匹配除“\r”“\n”之外的任何单个字符。 |
2.Java中正则表达式相关操作
Java中的一个简单示例,主要是Pattern和Matcher类的使用。示例参考2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class helloworld {
public static void main(String[] args) {
String lineString="QT3000! OK?";
String patternString = "(.*)(\\d+)(.*)";
//(.*)除\r、\n之外的字符0-无数次,(\\d+)数字1次上,
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(lineString);
//.group(0)表示模式本身,不计入groupCount
if(matcher.find()) {
System.out.println(matcher.groupCount());//3
System.out.println(matcher.group(0));//QT3000! OK?
System.out.println(matcher.group(1));//QT300
System.out.println(matcher.group(2));//0
System.out.println(matcher.group(3));//! OK?
}
int count = 0;
//输出原字符串中匹配开始位置和结束位置
String rgxString="\\d+";
Pattern rgxPattern = Pattern.compile(rgxString);
Matcher rgxMatcher = rgxPattern.matcher(lineString);
while(rgxMatcher.find()) {
count++;
System.out.println(count);//1
System.out.println("start(): "+rgxMatcher.start());//start(): 2
System.out.println("end(): "+rgxMatcher.end());//end(): 6
}
}
}
3.Python 中的正则表达式相关操作
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 28 23:28:43 2019
@author: lx
"""
import re
string="1 creates 2,2 creates 3,3 creates everything"
res1=re.findall(r'\d',string)
print(res1)#['1', '2', '2', '3', '3']
res2=re.split(r'\s',string)
print(res2)#['1', 'creates', '2,2', 'creates', '3,3', 'creates', 'everything']
res3=re.match(r"(\d+)(\s+)",string);
print(res3.groups())#('1', ' '),only the begin position
res3_1=re.match(r"(\d+)(\s+)","a1 creates 2,2 creates 3,3 creates everything")
if not res3_1:
print("No match")
res4=re.search(r"(\d+)(\s+)","a1 creates 2,2 creates 3,3 creates everything",re.M|re.I)
print(res4.groups())#('1', ' ')#The whole string,but only once.
phone = "0731-959-559 # This is Phone Number"
# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num #Phone Num : 0731-959-559
# Remove anything other than digits
num = re.sub(r'\D', "", "139-2387-3674")
print "Phone Num : ", num #Phone Num : 13923873674
pattern = re.compile(r'\d+') #create a Pattern object
m = pattern.match('one12twothree34four') # only the begin pos ,none match
print m #none
m = pattern.search('one12twothree34four')
print m.group() #12
- 引用
1.https://zh.wikipedia.org/wiki/正则表达式
2.https://www.tutorialspoint.com/java/java_regular_expressions.htm