正则表达式测试和解析工具
正则表达式
文本内容
匹配文本
代码示例
在常用编程语言中使用正则表达式。
JavaScript
是否匹配
// Check if the text matches
const isMatch = /pattern/.test(text);获取匹配内容
// Get the matched content
const match = text.match(/pattern/);遍历匹配与分组
// Iterate matches and groups
for (const m of text.matchAll(/pattern/g)) {
console.log(m[0], m.groups);
}TypeScript
是否匹配
// Check if the text matches
const isMatch: boolean = /pattern/.test(text);获取匹配内容
// Get the matched content
const match: RegExpMatchArray | null = text.match(/pattern/);遍历匹配与分组
// Iterate matches and groups
for (const m of text.matchAll(/pattern/g)) {
console.log(m[0], m.groups);
}Python
是否匹配
# Check if the text matches
import re
is_match = bool(re.search(r"pattern", text))获取匹配内容
# Get the matched content
m = re.search(r"pattern", text)遍历匹配与分组
# Iterate matches and groups
for m in re.finditer(r"pattern", text):
print(m.group(0), m.groups())Java
是否匹配
// Check if the text matches
Pattern p = Pattern.compile("pattern");
Matcher m = p.matcher(text);
boolean isMatch = m.find();获取匹配内容
// Get the matched content
String matched = m.group();遍历匹配与分组
// Iterate matches and groups
while (m.find()) {
System.out.println(m.group());
System.out.println(m.groupCount());
}C#
是否匹配
// Check if the text matches
Regex regex = new Regex("pattern");
bool isMatch = regex.IsMatch(text);获取匹配内容
// Get the matched content
Match match = regex.Match(text);
string matched = match.Value;遍历匹配与分组
// Iterate matches and groups
foreach (Match m in regex.Matches(text)) {
Console.WriteLine(m.Value);
foreach (Group g in m.Groups) Console.WriteLine(g.Value);
}Go
是否匹配
// Check if the text matches
re := regexp.MustCompile("pattern")
isMatch := re.MatchString(text)获取匹配内容
// Get the matched content
match := re.FindString(text)遍历匹配与分组
// Iterate matches and groups
for _, m := range re.FindAllStringSubmatch(text, -1) {
fmt.Println(m[0], m[1:])
}PHP
是否匹配
// Check if the text matches
$isMatch = (bool) preg_match("/pattern/", $text);获取匹配内容
// Get the matched content
preg_match("/pattern/", $text, $match);遍历匹配与分组
// Iterate matches and groups
preg_match_all("/pattern/", $text, $matches, PREG_SET_ORDER);
foreach ($matches as $m) { print_r($m); }Ruby
是否匹配
# Check if the text matches
is_match = !!(text =~ /pattern/)获取匹配内容
# Get the matched content
match = text.match(/pattern/)遍历匹配与分组
# Iterate matches and groups
text.scan(/pattern/) do |m|
puts m
endSwift
是否匹配
// Check if the text matches
let isMatch = text.range(of: "pattern", options: .regularExpression) != nil获取匹配内容
// Get the matched content
let range = text.range(of: "pattern", options: .regularExpression)遍历匹配与分组
// Iterate matches and groups
let regex = try NSRegularExpression(pattern: "pattern")
regex.enumerateMatches(in: text, range: NSRange(location: 0, length: text.utf16.count)) { m, _, _ in
print(m?.string ?? "")
}Kotlin
是否匹配
// Check if the text matches
val isMatch = Regex("pattern").containsMatchIn(text)获取匹配内容
// Get the matched content
val match = Regex("pattern").find(text)遍历匹配与分组
// Iterate matches and groups
for (m in Regex("pattern").findAll(text)) {
println(m.value)
println(m.groups)
}