返回
小鹏笔记 第二期(持续更新)
IOS
2023-10-16 09:04:54
JavaScript
JavaScript中使用正则表达式
const re = /ab+c/;
const str = "abbc";
console.log(re.test(str)); // true
将b
替换为B
const re = /ab+c/;
const str = "abbc";
const newStr = str.replace(re, "aBc");
console.log(newStr); // "aBc"
PHP
PHP中使用正则表达式
$re = '/ab+c/';
$str = 'abbc';
var_dump(preg_match($re, $str)); // int(1)
将b
替换为B
$re = '/ab+c/';
$str = 'abbc';
$newStr = preg_replace($re, 'aBc', $str);
echo $newStr; // "aBc"
Python
Python中使用正则表达式
import re
re_pattern = r'/ab+c/'
str = 'abbc'
match = re.search(re_pattern, str)
if match:
print('Match found')
将b
替换为B
import re
re_pattern = r'/ab+c/'
str = 'abbc'
new_str = re.sub(re_pattern, 'aBc', str)
print(new_str) # "aBc"
Java
Java中使用正则表达式
import java.util.regex.Pattern;
String re_pattern = "/ab+c/";
String str = "abbc";
Pattern pattern = Pattern.compile(re_pattern);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("Match found");
}
将b
替换为B
import java.util.regex.Pattern;
String re_pattern = "/ab+c/";
String str = "abbc";
Pattern pattern = Pattern.compile(re_pattern);
Matcher matcher = pattern.matcher(str);
new_str = matcher.replaceAll("aBc");
System.out.println(new_str); // "aBc"
C#
C#中使用正则表达式
using System.Text.RegularExpressions;
string re_pattern = "/ab+c/";
string str = "abbc";
Match match = Regex.Match(str, re_pattern);
if (match.Success) {
Console.WriteLine("Match found");
}
将b
替换为B
using System.Text.RegularExpressions;
string re_pattern = "/ab+c/";
string str = "abbc";
new_str = Regex.Replace(str, re_pattern, "aBc");
Console.WriteLine(new_str); // "aBc"
Node.js
Node.js中使用正则表达式
const re = /ab+c/;
const str = "abbc";
console.log(re.test(str)); // true
将b
替换为B
const re = /ab+c/;
const str = "abbc";
const newStr = str.replace(re, "aBc");
console.log(newStr); // "aBc"