Java 正则表达式基于 Perl 正则表达式实现的。
import java.util.regex.*\E、\l、\L、\u和\U进行大小写转换\b匹配退格符\z
通过java.util.regex.Matcher类
| 方法 | 描述 |
|---|---|
| 索引方法 | |
public int start() | 返回以前匹配的初始索引 |
public int start(int group) | 返回在以前的匹配操作期间,由给定组所捕获的子序列的初始索引 |
public int end() | 返回最后匹配字符之后的偏移量 |
public int end(int group) | 返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量 |
| 查找方法 | |
public boolean lookingAt() | 尝试将从区域开头开始的输入序列与该模式匹配 |
public boolean find() | 尝试查找与该模式匹配的输入序列的下一个子序列 |
public boolean find(int start) | 重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列 |
public boolean matches() | 尝试将整个区域与模式匹配 |
| 替换方法 | |
public Matcher appendReplacement(StringBuffer sb, String replacement) | 实现非终端添加和替换步骤 |
public StringBuffer appendTail(StringBuffer sb) | 实现终端添加和替换步骤 |
public String replaceAll(String replacement) | 替换模式与给定替换字符串相匹配的输入序列的每个子序列 |
public String replaceFirst(String replacement) | 替换模式与给定替换字符串匹配的输入序列的第一个子序列 |
public static String quoteReplacement(String s) | 返回指定字符串的字面替换字符串。这个方法返回一个字符串,就像传递给Matcher类的appendReplacement 方法一个字面字符串一样工作 |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld {
private static final String str = "qw qwjy qw qwjy";
private static final String rule = "\\bqw\\b";
public static void main(String []args) {
Pattern p = Pattern.compile(rule);
Matcher m = p.matcher(str); // 获取 matcher 对象
int cnt = 0;
m.find();
//while(m.find()){
System.out.println("编号:" + cnt ++);
System.out.println("start(): " + m.start());
System.out.println("end(): " + m.end());
System.out.println("start(cnt): " + m.start(cnt - 1));
System.out.println("end(cnt): " + m.end(cnt - 1));
//}
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld {
private static final String str1 = "qw qwjy qw qwjy";
private static final String str2 = "qwjy qw qwjy qw";
private static final String rule = "\\bqw\\b";
private static Pattern pattern;
private static Matcher matcher1;
private static Matcher matcher2;
public static void main(String []args) {
pattern = Pattern.compile(rule);
matcher1 = pattern.matcher(str1);
matcher2 = pattern.matcher(str2);
System.out.println("lookingAt(): " + matcher1.lookingAt());
System.out.println("matches(): " + matcher1.matches());
System.out.println("lookingAt(): " + matcher2.lookingAt());
matcher1.find(5);
System.out.println("start(): " + matcher1.start());
System.out.println("end(): " + matcher1.end());
}
}

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld {
private static String str = "qw qwjy qw qwjy";
private static String rule = "\\bqw\\b";
private static String replace = "qwcc";
public static void main(String []args) {
Pattern p = Pattern.compile(rule);
Matcher m = p.matcher(str);
str = m.replaceAll(replace);
System.out.println(str);
str = m.replaceFirst(replace);
System.out.println(str);
m = p.matcher(str);
StringBuffer s = new StringBuffer();
while(m.find()){
m.appendReplacement(s,"qwc");
}
m.appendTail(s);
System.out.println(s.toString());
}
}

JavaScript 的正则表达式支持源自于 Perl 语言。
\A和\Z。
RegExp 对象用于将文本与一个模式匹配。
new RegExp(pattern[, flags])
RegExp(pattern[, flags])
pattern
正则表达式的文本。从 ES5 开始,这也可以是另一个RegExp对象或文字 (仅用于两个 RegExp 构造函数符号)。模式可以包含特殊字符special characters来匹配比字面值字符串更广泛的值范围。
flags
如果指定,flags 是包含要添加的标志的字符串。
或者,如果为模式提供了一个对象,flags 字符串将替换该对象的任何标志 (并且lastIndex将重置为 0)(从 ES2015 开始)。
如果没有指定flags并且提供了一个正则表达式对象,则该对象的 flags(和 lastIndex 值) 将被复制。
- g (全局匹配)找到所有的匹配,而不是在第一个匹配之后停止。
- i (忽略大小写)如果u标志也被启用,使用 Unicode 大小写折叠。
- m (多行匹配)将开始和结束字符 (^ and $) 视为在多行上工作。换句话说,匹配每一行的开头或结尾each line (由\n或者\r 分隔),而不仅仅是整个输入字符串的开头或结尾。
- s (点号匹配所有字符)允许. 去匹配新的行。
- u (统一码)将模式视为一系列 Unicode 代码点。
- y (sticky,粘性匹配)仅匹配目标字符串中此正则表达式的 lastIndex 属性指示的索引。不尝试从任何以后的索引进行匹配。
| 方法 | 描述 |
|---|---|
exec | 一个在字符串中执行查找匹配的 RegExp 方法,它返回一个数组(未匹配到则返回 null) |
test | 一个在字符串中测试是否匹配的 RegExp 方法,它返回 true 或 false |
match | 一个在字符串中执行查找匹配的 String 方法,它返回一个数组,在未匹配到时会返回 null |
matchAll | 一个在字符串中执行查找所有匹配的 String 方法,它返回一个迭代器(iterator) |
search | 一个在字符串中测试匹配的 String 方法,它返回匹配到的位置索引,或者在失败时返回-1 |
replace | 一个在字符串中执行查找匹配的 String 方法,并且使用替换字符串替换掉匹配到的子字符串 |
split | 一个使用正则表达式或者一个固定字符串分隔一个字符串,并将分隔后的子字符串存储到数组中的 String 方法 |
const regex1 = RegExp('\\bqw\\b', 'g');
const str1 = 'qw qwjy qw qwjy';
let array1;
while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// expected output: "Found qw. Next starts at 9."
// expected output: "Found qw. Next starts at 19."
}
console.log(regex1.test(str1));
// expected output: true
console.log(regex1.test("cc"));
// expected output: false
const found = str1.match(regex1);
console.log(found);
// expected output: ["qw", "qw"]
const array = [...str1.matchAll(/q(w)(jy)/g)];
console.log(array[0]);
// expected output: ["qwjy", "w", "jy"]
console.log(str1.search(regex1));
// expected output: 0
console.log(str1[str1.search(regex1)]);
// expected output: "q"
console.log(str1.replace('qw', 'qwcc'));
// expected output: "qwcc qwjy qw qwjy"
console.log(str1.split(' '));
// expected output: ["qw", "qwjy", "qw", "qwjy"]

\a、\b、\e、\f和\v。
MySql 允许在 WHERE 子句中使用正则表达式匹配:
REGEXP "expression"
SELECT * FROM table WHERE REGEXP "qwjy"
Python 通过 re 模块提供了正则表达式。
re.compile 将正则表达式编译成对象。re.compiler接受可选得标志,例如 re.IGNORECASE(表示搜索得时候不区分字母大小写)。re.VERBOSE 可协助调试正则表达式。
| 方法 | 描述 |
|---|---|
preg_grep() | 执行搜索并以数组形式返回匹配结果 |
findall(string[, pos[, endpos]]) | 查找所有子串并以列表形式将其返回string: 待匹配的字符串pos: 可选参数,指定字符串的起始位置,默认为 0endpos: 可选参数,指定字符串的结束位置,默认为字符串的长度 |
finditer(pattern, string, flags=0) | 查找所有子串并以迭代器形式将其返回pattern: 匹配的正则表达式string: 要匹配的字符串flags: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等 |
match(pattern, string, flags=0) | 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match() 就返回 none。pattern: 匹配的正则表达式string: 要匹配的字符串flags: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等 |
search(pattern, string, flags=0) | 扫描整个字符串并返回第一个成功的匹配。pattern: 匹配的正则表达式string: 要匹配的字符串flags: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等 |
split(pattern, string[, maxsplit=0, flags=0]) | 将字符串转换成列表,在模式匹配的地方将其分割pattern: 匹配的正则表达式string: 要匹配的字符串maxsplit: 分隔次数,maxsplit=1 分隔一次,默认为 0,不限制次数flags: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等 |
sub(pattern, repl, string, count=0, flags=0) | 用指定的子串替换匹配项pattern: 正则中的模式字符串repl: 替换的字符串,也可为一个函数string: 要被查找替换的原始字符串count: 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配 |
subn() | 返回一个字符串,其中匹配项被指定的子串替换 |
compile(pattern[, flags]) | 用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用pattern: 一个字符串形式的正则表达式flags: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等 |
match 只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回 None;而 search 匹配整个字符串,直到找到一个匹配。
| 修饰符 | 描述 |
|---|---|
re.I | 使匹配对大小写不敏感 |
re.L | 做本地化识别(locale-aware)匹配 |
re.M | 多行匹配,影响 ^ 和 $ |
re.S | 使 . 匹配包括换行在内的所有字符 |
re.U | 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B. |
re.X | 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解 |
import re
print(re.match('qw', 'qw qwjy qwcc').span()) # 在起始位置匹配
print(re.match('qwcc', 'qw qwjy qwcc')) # 不在起始位置匹配
print(re.search('qw', 'qw qwjy qwcc').span()) # 在起始位置匹配
print(re.search('qwcc', 'qw qwjy qwcc').span()) # 不在起始位置匹配
num = re.sub(r'qw', "", "qw qwjy qwcc") # 删除qw
print(num)
pattern = re.compile(r'qw')
m = pattern.match('qw qwjy qwcc')
print(m.span())
result = pattern.findall('qw qwjy qwcc')
print(result)
it = re.finditer(r"qw","qw qwjy qwcc")
for match in it:
print (match.group() )
print(re.split('\s+', 'qw qwjy qwcc'))
