正则表达式在各种编程语言中的基本使用
下面是不同编程语言中使用正则表达式的基本代码示例:
JavaScript
// 创建正则表达式
const regex1 = /pattern/flags; // 字面量形式
const regex2 = new RegExp('pattern', 'flags'); // 构造函数形式
// 示例:匹配测试
const str = "Hello World";
const testResult = /world/i.test(str); // true (不区分大小写)
// 示例:提取匹配
const matchResult = str.match(/hello/i); // ["Hello"]
// 示例:替换
const replaced = str.replace(/world/i, "Universe"); // "Hello Universe"
// 示例:分割
const parts = "a,b,c".split(/,/); // ["a", "b", "c"]
Python
import re
# 匹配测试
if re.search(r'pattern', 'string'):
print("Found a match!")
# 示例:查找所有匹配
matches = re.findall(r'\d+', '123 abc 456 def') # ['123', '456']
# 示例:替换
replaced = re.sub(r'\s+', '-', 'split this string') # "split-this-string"
# 示例:分割
parts = re.split(r'\s+', 'split this string') # ['split', 'this', 'string']
# 编译正则表达式(多次使用时效率更高)
pattern = re.compile(r'\d{3}-\d{2}-\d{4}')
if pattern.search('123-45-6789'):
print("Valid SSN format")
Java
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// 编译正则表达式
Pattern pattern = Pattern.compile("\\d+");
// 创建匹配器
Matcher matcher = pattern.matcher("123 abc 456 def");
// 查找所有匹配
while (matcher.find()) {
System.out.println("Found: " + matcher.group());
}
// 匹配测试
boolean matches = Pattern.matches("a*b", "aaaaab"); // true
// 替换
String replaced = "a b c".replaceAll("\\s", "-"); // "a-b-c"
// 分割
String[] parts = "a,b,c".split(","); // ["a", "b", "c"]
}
}
C#
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
// 匹配测试
if (Regex.IsMatch("Hello World", @"world", RegexOptions.IgnoreCase)) {
Console.WriteLine("Found a match!");
}
// 查找匹配
Match match = Regex.Match("123 abc 456", @"\d+");
if (match.Success) {
Console.WriteLine("First number: " + match.Value); // "123"
}
// 查找所有匹配
MatchCollection matches = Regex.Matches("123 abc 456", @"\d+");
foreach (Match m in matches) {
Console.WriteLine(m.Value); // "123", then "456"
}
// 替换
string replaced = Regex.Replace("Hello World", "World", "Universe"); // "Hello Universe"
// 分割
string[] parts = Regex.Split("a,b,c", ","); // ["a", "b", "c"]
}
}
PHP
<?php
// 匹配测试
if (preg_match('/\d+/', '123 abc')) {
echo "Found a number";
}
// 提取所有匹配
preg_match_all('/\d+/', '123 abc 456 def', $matches);
print_r($matches[0]); // Array ( [0] => 123 [1] => 456 )
// 替换
$replaced = preg_replace('/\s+/', '-', 'split this string'); // "split-this-string"
// 分割
$parts = preg_split('/\s+/', 'split this string'); // Array ( [0] => split [1] => this [2] => string )
// 使用命名捕获组
preg_match('/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/', '2023-05-15', $dateMatches);
echo $dateMatches['year']; // "2023"
echo $dateMatches['month']; // "05"
echo $dateMatches['day']; // "15"
?>
Ruby
# 匹配测试
if "Hello World" =~ /world/i
puts "Found a match!"
end
# 提取匹配
match_data = /(\d+).*(\d+)/.match("123 abc 456")
puts match_data[1] # "123"
puts match_data[2] # "456"
# 替换
replaced = "Hello World".gsub(/world/i, "Universe") # "Hello Universe"
# 扫描所有匹配
numbers = "123 abc 456 def".scan(/\d+/) # ["123", "456"]
# 使用命名捕获组
if /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ =~ "2023-05-15"
puts year # "2023"
puts month # "05"
puts day # "15"
end
Perl
# 匹配测试
if ("Hello World" =~ /world/i) {
print "Found a match!\n";
}
# 提取匹配
if ("123 abc 456" =~ /(\d+).*(\d+)/) {
print "First number: $1\n"; # "123"
print "Second number: $2\n"; # "456"
}
# 替换
$str = "Hello World";
$str =~ s/world/universe/gi; # "Hello Universe"
print "$str\n";
# 使用命名捕获组
if ("2023-05-15" =~ /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) {
print "Year: $+{year}\n"; # "2023"
print "Month: $+{month}\n"; # "05"
print "Day: $+{day}\n"; # "15"
}
Go
package main
import (
"fmt"
"regexp"
)
func main() {
// 编译正则表达式
re := regexp.MustCompile(`\d+`)
// 匹配测试
fmt.Println(re.MatchString("123 abc")) // true
// 查找匹配
match := re.FindString("123 abc 456") // "123"
fmt.Println(match)
// 查找所有匹配
matches := re.FindAllString("123 abc 456 def", -1) // ["123", "456"]
fmt.Println(matches)
// 替换
replaced := re.ReplaceAllString("123 abc 456", "###") // "### abc ###"
fmt.Println(replaced)
// 分割
parts := regexp.MustCompile(`\s+`).Split("split this string", -1) // ["split", "this", "string"]
fmt.Println(parts)
// 使用子匹配
dateRe := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`)
submatches := dateRe.FindStringSubmatch("2023-05-15") // ["2023-05-15", "2023", "05", "15"]
fmt.Println(submatches[1:]) // ["2023", "05", "15"]
}
Swift
import Foundation
let str = "Hello World 123"
// 创建正则表达式
let regex = try! NSRegularExpression(pattern: "\\d+")
// 匹配测试
let range = NSRange(location: 0, length: str.utf16.count)
if regex.firstMatch(in: str, options: [], range: range) != nil {
print("Found a match")
}
// 查找所有匹配
let matches = regex.matches(in: str, range: range)
for match in matches {
let matchRange = match.range
if let substringRange = Range(matchRange, in: str) {
let substring = str[substringRange]
print("Found: \(substring)") // "123"
}
}
// 替换
let replaced = regex.stringByReplacingMatches(in: str, range: range, withTemplate: "###")
print(replaced) // "Hello World ###"
Kotlin
fun main() {
val text = "Hello World 123"
// 创建正则表达式
val regex = Regex("\\d+")
// 匹配测试
println(regex.containsMatchIn(text)) // true
// 查找第一个匹配
println(regex.find(text)?.value) // "123"
// 查找所有匹配
regex.findAll(text).forEach { match ->
println(match.value) // "123"
}
// 替换
println(regex.replace(text, "###")) // "Hello World ###"
// 分割
println(Regex("\\s+").split("split this string")) // ["split", "this", "string"]
// 使用分组
val dateRegex = Regex("(\\d{4})-(\\d{2})-(\\d{2})")
val matchResult = dateRegex.find("2023-05-15")!!
println(matchResult.groupValues) // ["2023-05-15", "2023", "05", "15"]
}
Bash
#!/bin/bash
string="Hello World 123"
# 使用 =~ 进行匹配
if [[ $string =~ [0-9]+ ]]; then
echo "Found numbers: ${BASH_REMATCH[0]}" # "123"
fi
# 使用 grep
echo "$string" | grep -Eo '[0-9]+' # "123"
# 使用 sed 替换
echo "$string" | sed -E 's/[0-9]+/###/' # "Hello World ###"
# 使用 awk 提取
echo "$string" | awk '{match($0, /[0-9]+/); print substr($0, RSTART, RLENGTH)}' # "123"